Class MyTcpListener
Public Shared Sub Main()
Try
' Set the TcpListener on port 13000.
' Buffer for reading data
Dim bytes As Byte()
' Enter the listening loop.
'While True
If Mainfrm.txtlbl.Text = String.Empty Then
Mainfrm.txtlbl.Text = ("Waiting for a connection... ")
Mainfrm.txtlbl.ForeColor = Color.Orange
End If
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
'If Not server.AcceptTcpClient Is Nothing Then
If server.Pending = True Then
Dim client As TcpClient = server.AcceptTcpClient()
cmand = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
ReDim bytes(1000) 'stream.ToString.Length)
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
cmand = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Mainfrm.txtlbl.Text = (NewLine & cmand)
' Process the data sent by the client.
cmand = cmand '.ToUpper()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes("Message Received")
' Send back a response.
stream.Write(msg, 0, msg.Length)
i = stream.Read(bytes, 0, bytes.Length)
End While
cmd = cmand
' Shutdown and end connection
client.Close()
'End If
'Else
'server.Stop()
End If
'End While
Catch e As SocketException
MessageBox.Show("Error: " & e.Message)
End Try
End Sub 'Main
End Class 'MyTcpListener