Hi All,
I am developing a client server communication using system.net.socket and I am finding it very difficult to get a solution for this. I started with single port communication with single client and as I was asked to develop to listen on multiple ports handling multiple clients, I am stuck with out a solution. The below is what could end up doing any help to solve the problem will be very much appreciated.
Below is the code I tried but it does not allow a single client on multiple requests.
-
Class MyTcpListener
-
Public Shared stream As NetworkStream
-
Public Shared Sub Main()
-
Dim port As Int32 = 1006
-
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
-
Dim server As TcpListener = Nothing
-
server = New TcpListener(localAddr, port)
-
server.Start()
-
DoBeginAcceptSocket(server)
-
End Sub 'Main
-
' Thread signal.
-
Public Shared clientConnected As New ManualResetEvent(False)
-
-
-
' Accept one client connection asynchronously.
-
Public Shared Sub DoBeginAcceptSocket(ByVal listener As TcpListener)
-
' Set the event to nonsignaled state.
-
clientConnected.Reset()
-
-
' Start to listen for connections from a client.
-
Console.WriteLine("Waiting for a connection...")
-
-
' Accept the connection.
-
' BeginAcceptSocket() creates the accepted socket.
-
-
listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener)
-
' Wait until a connection is made and processed before
-
' continuing.
-
-
clientConnected.WaitOne()
-
End Sub 'DoBeginAcceptSocket
-
-
-
' Process the client connection.
-
Public Shared Sub DoAcceptSocketCallback(ByVal ar As IAsyncResult)
-
' Get the listener that handles the client request.
-
Dim returnvalue As IAsyncResult
-
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
-
Dim client As Socket
-
client = listener.BeginAcceptSocket(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener)
-
' End the operation and display the received data on the
-
'console.
-
Dim bytes(1024) As Byte
-
-
returnValue = stream.BeginRead(bytes, 0, bytes.Length, New AsyncCallback(AddressOf myReadCallBack), stream)
-
' Process the connection here. (Add the client to a
-
' server table, read data, etc.)
-
Console.WriteLine("Client connected completed")
-
Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
-
' Signal the calling thread to continue.
-
clientConnected.Set()
-
End Sub 'DoAcceptSocketCallback
-
-
Public Shared Sub myReadCallBack(ByVal ar As IAsyncResult)
-
-
Dim myNetworkStream As NetworkStream = CType(ar.AsyncState, NetworkStream)
-
Dim myReadBuffer(1024) As Byte
-
Dim myCompleteMessage As [String] = ""
-
Dim numberOfBytesRead As Integer
-
-
numberOfBytesRead = myNetworkStream.EndRead(ar)
-
myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
-
-
' message received may be larger than buffer size so loop through until you have it all.
-
While myNetworkStream.DataAvailable
-
-
myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New AsyncCallback(AddressOf myReadCallBack), myNetworkStream)
-
End While
-
-
-
' Print out the received message to the console.
-
Console.WriteLine(("You received the following message : " + myCompleteMessage))
-
End Sub 'myReadCallBack
-
-
-
End Class 'MyTcpListener
In deep trouble