Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
i have a client connecting to a server. they exchange mesages fine the first time, but i dont know how to keep the server listening for a client indefinately. anyone give any pointers on this?
Posted
The .AcceptSocket method blocks until a connection is created. The method returns a socket. You need to pass this socket off to a different process that fires on its own thread.
private void StartServer()
{
   TcpListener server = new TcpListener(IPAddress.Any, 1234);
   server.Start();

   Socket socket;

   ChatSession chatSession;
   
   while (true)
   {
       // blocks until a connection is established
       socket = server.AcceptSocket();

       // when this fires the socket will be connected and the connection 
       // will actually be setup on a port other than 1234.  this allows
       // us to keep servicing future requests on port 1234.
       
       // for long running processes, the key is to somehow pass the socket 
       // off to another thread.  if you don't do this the server will
       // not be able to establish new connections while the process is still
       // running.

       chatSession = new ChatSession(socket);
       chatSessions.Add(chatSession);
   }
}

The "ChatSession" class would spawn a new thread to handle the communication on the socket, which would allow the while loop to iterate again and wait for a new incoming connection request.

  • 2 months later...
Posted

VB Compatable

 

Gill Bates,

I was looking for this same info but for VB. Can you show me how to do this in VB. I too want to serve multiple clients simultaniously. I used to accomplish this with multiple winsock controls in VB6. I haven't done it in .net. Thanks in advance.

Ignorance begins with I.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...