469,358 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

tcpclient server in async mode accepting multiple clients and on multiple ports

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.
Expand|Select|Wrap|Line Numbers
  1. Class MyTcpListener
  2.     Public Shared stream As NetworkStream
  3.     Public Shared Sub Main()
  4.         Dim port As Int32 = 1006
  5.         Dim localAddr As IPAddress = IPAddress.Parse("")
  6.         Dim server As TcpListener = Nothing
  7.         server = New TcpListener(localAddr, port)
  8.         server.Start()
  9.         DoBeginAcceptSocket(server)
  10.     End Sub 'Main
  11.     ' Thread signal.
  12.     Public Shared clientConnected As New ManualResetEvent(False)
  15.     ' Accept one client connection asynchronously.
  16.     Public Shared Sub DoBeginAcceptSocket(ByVal listener As TcpListener)
  17.         ' Set the event to nonsignaled state.
  18.         clientConnected.Reset()
  20.         ' Start to listen for connections from a client.
  21.         Console.WriteLine("Waiting for a connection...")
  23.         ' Accept the connection. 
  24.         ' BeginAcceptSocket() creates the accepted socket.
  26.         listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener)
  27.         ' Wait until a connection is made and processed before 
  28.         ' continuing.
  30.         clientConnected.WaitOne()
  31.     End Sub 'DoBeginAcceptSocket
  34.     ' Process the client connection.
  35.     Public Shared Sub DoAcceptSocketCallback(ByVal ar As IAsyncResult)
  36.         ' Get the listener that handles the client request.
  37.         Dim returnvalue As IAsyncResult
  38.         Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
  39.         Dim client As Socket
  40.         client = listener.BeginAcceptSocket(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener)
  41.         ' End the operation and display the received data on the
  42.         'console.
  43.         Dim bytes(1024) As Byte
  45.         returnValue = stream.BeginRead(bytes, 0, bytes.Length, New AsyncCallback(AddressOf myReadCallBack), stream)
  46.         ' Process the connection here. (Add the client to a 
  47.         ' server table, read data, etc.)
  48.         Console.WriteLine("Client connected completed")
  49.         Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
  50.         ' Signal the calling thread to continue.
  51.         clientConnected.Set()
  52.     End Sub 'DoAcceptSocketCallback
  54.     Public Shared Sub myReadCallBack(ByVal ar As IAsyncResult)
  56.         Dim myNetworkStream As NetworkStream = CType(ar.AsyncState, NetworkStream)
  57.         Dim myReadBuffer(1024) As Byte
  58.         Dim myCompleteMessage As [String] = ""
  59.         Dim numberOfBytesRead As Integer
  61.         numberOfBytesRead = myNetworkStream.EndRead(ar)
  62.         myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
  64.         ' message received may be larger than buffer size so loop through until you have it all.
  65.         While myNetworkStream.DataAvailable
  67.             myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New AsyncCallback(AddressOf myReadCallBack), myNetworkStream)
  68.         End While
  71.         ' Print out the received message to the console.
  72.         Console.WriteLine(("You received the following message : " + myCompleteMessage))
  73.     End Sub 'myReadCallBack
  76. End Class 'MyTcpListener
In deep trouble
Jan 8 '10 #1
4 6161
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Jan 8 '10 #2
9,735 Expert Mod 8TB
I recommend implementing a class that manages the connection alone that is separate from the class listens for new connections. The class that listens for connections will have to have a collection of classes that manages the connection so that when a new connection is made it can add a new connection to the collection.

For example, you would have to implement a class...let's call it "AsyncClientConnection" that manages the connection. This class manages the asynchronous socket connection logic. It is responsible for sending and receiving data from the a the client that the connection is for.

You would also have to implement a class that manages the connections...let's call it the "SocketServerManager". This class manages the connected AsyncClientConnections. It has a collection (an array, or list of) of AsyncClientConnections. The SocketServerManager accepts incoming asynchronous socket connections and creates new AsyncClientConnections (that manages the connection) for each connection and adds it to the collection. It also removes any AsyncClientConnections that are no longer connected every so often to clean up unused resources.

Jan 8 '10 #3
Thanks Frinny,

I finally found the solution The server now has 2 classes one class handling new client requests and in that class we call another class which connects already connected client.

Both the classes have methods which run with infinite loops which makes handling any number of requests from multiple clients using the same port

thanks again
Jan 9 '10 #4
can you send me the code, i need your help. pls post here or send to oghenez @ gmail dot com
Jun 8 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ssantamariagarcia | last post: by
3 posts views Thread by Sagaert Johan | last post: by
4 posts views Thread by SRLoka | last post: by
5 posts views Thread by Jeff Weber | last post: by
1 post views Thread by Jeff Weber | last post: by
4 posts views Thread by Peter | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.