Ontani Posted June 6, 2005 Posted June 6, 2005 Hey i got a simple tcp server/client application. and i have the listening on the port 6666 in a thread. it keeps listening till the client connects and then it closes the thread. but i want it to keep the thread running so that another client can connect. Const portNumber As Integer = 6666 Dim tcpListener As New tcpListener(portNumber) Dim listenThread As System.Threading.Thread Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Must listen on correct port- must be same as port client wants to connect on. tcpListener.Start() listenThread = New System.Threading.Thread(AddressOf doListen) listenThread.IsBackground = True listenThread.Start() Debug.WriteLine("Waiting for connection...") End Sub Public Sub doListen() 'Accept the pending client connection and return 'a TcpClient initialized for communication. Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient() Debug.WriteLine("Connection accepted.") ' Get the stream Dim networkStream As NetworkStream = tcpClient.GetStream() ' Read the stream into a byte array Dim bytes(tcpClient.ReceiveBufferSize) As Byte networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) ' Return the data received from the client to the console. Dim clientdata As String = Encoding.ASCII.GetString(bytes) Console.WriteLine(("Client sent: " + clientdata)) Dim responseString As String = "Connected to server." Dim sendBytes As [byte]() = Encoding.ASCII.GetBytes(responseString) networkStream.Write(sendBytes, 0, sendBytes.Length) Console.WriteLine(("Message Sent /> : " + responseString)) 'Any communication with the remote client using the TcpClient can go here. 'Close TcpListener and TcpClient. tcpClient.Close() Console.WriteLine("exit") End Sub thats my server code this is my client code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim tcpClient As New System.Net.Sockets.TcpClient() tcpClient.Connect("127.0.0.1", 6666) Dim networkStream As NetworkStream = tcpClient.GetStream() If networkStream.CanWrite And networkStream.CanRead Then ' Do a simple write. Dim sendBytes As [byte]() = Encoding.ASCII.GetBytes("Is anybody there") networkStream.Write(sendBytes, 0, sendBytes.Length) ' Read the NetworkStream into a byte buffer. Dim bytes(tcpClient.ReceiveBufferSize) As Byte networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) ' Output the data received from the host to the console. Dim returndata As String = Encoding.ASCII.GetString(bytes) Console.WriteLine(("Host returned: " + returndata)) Else If Not networkStream.CanRead Then Console.WriteLine("cannot not write data to this stream") tcpClient.Close() Else If Not networkStream.CanWrite Then Console.WriteLine("cannot read data from this stream") tcpClient.Close() End If End If End If Catch d As Exception MessageBox.Show(d.ToString) End Try End Sub my server debug: Waiting for connection... Connection accepted. Client sent: Is anybody there The thread '<No Name>' (0x105c) has exited with code 0 (0x0). Greetz Quote www.purevision.be :: www.devpoint.be
Administrators PlausiblyDamp Posted June 10, 2005 Administrators Posted June 10, 2005 You would need to implement some form of loop so when you have finished with a client you would go back and start waiting for another client to connect. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.