Imports System.Net.Sockets
Public Class Form1
Dim currentstr() As Byte
'Dim invited As List(Of String) = New List(Of String) 'ip addresses
Dim ip(3) As Byte
Dim conn As NetworkStream
'Dim s As Socket
Dim tcpclient As TcpClient
Dim tcplistener As TcpListener
Dim listenert As Threading.Thread = New Threading.Thread(AddressOf TcpListen)
Dim isclosing As Boolean = False
Delegate Sub NCDeleg(ByVal txtadd As String)
Dim ncinvoke As NCDeleg = New NCDeleg(AddressOf NotifyConn)
Private Sub TcpListen()
[B]tcplistener.Start()[/B]
While Not isclosing
If tcplistener.Pending() Then
Dim rs As NetworkStream = tcplistener.AcceptTcpClient().GetStream()
Dim b As Byte = rs.ReadByte
Dim str As String = ""
While b > 0
str &= ChrW(b)
b = rs.ReadByte()
End While
Me.Invoke(ncinvoke, str)
End If
Threading.Thread.Sleep(500)
End While
tcplistener.Stop()
End Sub
Private Sub NotifyConn(ByVal txtadd As String)
Me.txtChatLog.Text &= String.Format("{0}{1}{0}{0}", ControlChars.CrLf, txtadd)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
isclosing = True
listenert.Join()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set up form
Dim si As Size = Screen.GetWorkingArea(New Point(1, 1)).Size
Me.MaximumSize = New Size(si.Width \ 3, si.Height)
Me.Width = Me.MaximumSize.Width
'Retrieve chat IP.
Dim ipre As New System.Text.RegularExpressions.Regex("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
Dim cip As String = InputBox("Please enter the IP address of the person that you would like to send to.", "Enter IP Address", "0.0.0.0")
If Not ipre.IsMatch(cip) Then
Me.Close()
Exit Sub
End If
Dim ipbs() As String = cip.Split(".")
ip(0) = CByte(ipbs(0))
ip(1) = CByte(ipbs(1))
ip(2) = CByte(ipbs(2))
ip(3) = CByte(ipbs(3))
''Set up sockets
's = New Socket(AddressFamily.DataLink, SocketType.Stream, ProtocolType.Tcp)
's.Connect(New Net.IPAddress(ip), 120)
'conn = New NetworkStream(s, IO.FileAccess.ReadWrite, True)
tcpclient = New TcpClient()
Try
tcpclient.Connect(New Net.IPAddress(ip), 445)
Catch ex As SocketException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
Exit Sub
End Try
'tcplistener = New TcpListener(New Net.IPAddress(ip), 120)
conn = tcpclient.GetStream()
'conn.ReadTimeout = 10
tcplistener = New TcpListener(New Net.IPAddress(ip), 445)
listenert.Start()
'Me.tmrCheckForMessage.Start()
End Sub
'Private Sub btnInvite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvite.Click
' Dim ip As New System.Text.RegularExpressions.Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
' Dim val As String = InputBox("Please enter the IP address of the person that you would like to send to.", "Enter IP Address", "0.0.0.0")
' If Not ip.IsMatch(val) Then
' If val = "" Then Exit Sub
' MessageBox.Show("Invalid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Error)
' Exit Sub
' End If
' invited.Add(val)
'End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SendMsg(Me.txtNewMessage.Text)
Me.txtNewMessage.Text = ""
End Sub
Private Sub SendMsg(ByVal msg As String)
Dim c As Char
For Each c In msg
conn.WriteByte(AscW(c))
Next
conn.WriteByte(0)
End Sub
' Private Function GetMsgIfAny() As String
' On Error GoTo sub_exit
' Dim b As Byte = conn.ReadByte()
' Dim str As String = ""
' While b > 0
' str &= ChrW(b)
' b = conn.ReadByte()
' End While
' Return str
'sub_exit:
' GetMsgIfAny = ""
' End Function
'Private Sub GetAsyncMsg(ByVal ia As IAsyncResult)
' If ia.IsCompleted Then
' End If
'End Sub
'Private Sub tmrCheckForMessage_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCheckForMessage.Tick
' Dim s As String = GetMsgIfAny()
' If s = "" Then Exit Sub
' Me.txtChatLog.Text &= String.Format("{0}{1}{0}{0}", ControlChars.CrLf, s)
' conn.BeginRead(currentstr, 0, 1, AddressOf GetAsyncMsg, Nothing)
'End Sub
End Class