Hi again,
OK here is some code I nicked from web and I have changed some things, it works but only accepting one client, how do I make this accept multiple clients?
Code:Imports System.Net.Sockets Imports System.Text lass TCPSrv Shared Sub Main() ' Must listen on correct port- must be same as port client wants to connect on. Const portNumber As Integer = 29999 Dim tcpListener As New TcpListener(portNumber) tcpListener.Start() Console.WriteLine("Waiting for connection...") Try 'Accept the pending client connection and return a TcpClient initialized for communication. Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient() Console.WriteLine("Connection accepted.") Do While tcpClient.Connected = True ' Get the stream Dim networkStream As NetworkStream = tcpClient.GetStream() ' Read the stream into a byte array Dim bytes(tcpClient.ReceiveBufferSize) As Byte Dim bytecount As Integer = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) If bytecount > 0 Then ' Return the data received from the client to the console. Dim clientdata As String = Encoding.ASCII.GetString(bytes, 0, bytecount) Console.WriteLine(clientdata) Dim responseString As String = "OK" Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString) networkStream.Write(sendBytes, 0, sendBytes.Length) Console.WriteLine(responseString) 'Any communication with the remote client using the TcpClient can go here. 'Close TcpListener and TcpClient. Else tcpClient.Close() End If Loop Console.WriteLine("Client disconnected") Catch e As Exception tcpListener.Stop() Console.WriteLine(e.ToString()) End Try End Sub End Class
Thanks and sorry for asking probably stupid questions!
Steve




Reply With Quote