TcpListener Class : TCP Server « Network Remote « VB.Net






TcpListener Class

 

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic


Class MyTcpListener

    Public Shared Sub Main()
  Dim server As TcpListener = nothing
        Try
         Dim port As Int32 = 13000
         Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
         server = New TcpListener(localAddr, port)
         server.Start()
         Dim bytes(1024) As Byte
         Dim data As String = Nothing
         While True
            Dim client As TcpClient = server.AcceptTcpClient()
            data = Nothing
            Dim stream As NetworkStream = client.GetStream()
            Dim i As Int32
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
               data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
               data = data.ToUpper()
               Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
               stream.Write(msg, 0, msg.Length)
               i = stream.Read(bytes, 0, bytes.Length)
            End While
            client.Close()
         End While
      Catch e As SocketException
         Console.WriteLine("SocketException: {0}", e)
      Finally
         server.Stop()
      End Try
      Console.Read()
   End Sub 
End Class 

   
  








Related examples in the same category

1.TCP Server socket
2.TCP Server Demo