By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,703 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,703 IT Pros & Developers. It's quick & easy.

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

P: 2
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("127.0.0.1")
  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)
  13.  
  14.  
  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()
  19.  
  20.         ' Start to listen for connections from a client.
  21.         Console.WriteLine("Waiting for a connection...")
  22.  
  23.         ' Accept the connection. 
  24.         ' BeginAcceptSocket() creates the accepted socket.
  25.  
  26.         listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf DoAcceptSocketCallback), listener)
  27.         ' Wait until a connection is made and processed before 
  28.         ' continuing.
  29.  
  30.         clientConnected.WaitOne()
  31.     End Sub 'DoBeginAcceptSocket
  32.  
  33.  
  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
  44.  
  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
  53.  
  54.     Public Shared Sub myReadCallBack(ByVal ar As IAsyncResult)
  55.  
  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
  60.  
  61.         numberOfBytesRead = myNetworkStream.EndRead(ar)
  62.         myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
  63.  
  64.         ' message received may be larger than buffer size so loop through until you have it all.
  65.         While myNetworkStream.DataAvailable
  66.  
  67.             myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, New AsyncCallback(AddressOf myReadCallBack), myNetworkStream)
  68.         End While
  69.  
  70.  
  71.         ' Print out the received message to the console.
  72.         Console.WriteLine(("You received the following message : " + myCompleteMessage))
  73.     End Sub 'myReadCallBack
  74.  
  75.  
  76. End Class 'MyTcpListener
In deep trouble
Jan 8 '10 #1
Share this Question
Share on Google+
4 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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

Frinavale
Expert Mod 5K+
P: 9,731
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.

-Frinny
Jan 8 '10 #3

P: 2
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

P: 1
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.