Re: Sockets programming

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Scott McPhillips [MVP] (org-dot-mvps-at-scottmcp)
Date: 01/16/05


Date: Sun, 16 Jan 2005 10:07:53 -0500

Bonj wrote:
> I've been following a socket programming tutorial to make a simple TCP
> communication program, seemingly without hitches, it appears to work fine.
> However the structure of it is to have a server listening for requests from
> a client using listen(), and when one connects, it communicates with that
> client but only that one. It doesn't listen for more requests.
>
> What I'm wondering is can I have a server that continually listens for
> requests from multiple clients, and when *any* client connects, it can
> communicate with that client. I'm thinking multithreading is obviously going
> to be necessary in some form, but basically my questions are:
>
> 1) Can the server use a method similar to that described in the second
> paragraph above to communicate with multiple clients at the same time on the
> same port, or does it have to use a different port for each client that's
> simultaneously communicating?
> My initial hunch tells me that I can - because a web server can do it, but
> is there anything special to watch out for such as how it has to integrate
> with (2),...
>
> 2) Is it necessary to have multithreading, e.g. the server spawns a new
> thread for each client that connects in order to communicate with that
> client.
>
> Sorry if I've overly crossposted but this seems to be relevant to MS C++ and
> standard C++ as I'm intending to use it in linux aswell as windows, (the
> only thing seemingly different in linux seems to be that it has different
> includes and doesn't have to call WSAStartup).
>
>
>

Yes, of course a server can communicate with multiple clients on the
same port. You design your program so that while some clients are
connected additional notifications from listen() can still be processed.
  The key to this is to use asynchronous sockets, not synchronous. If
the tutorial you are using uses synchronous sockets then move on to
learning about asynchronous.

It is not necessay to use multithreading. The network handles only one
message at a time and it is possible to write an asynchronous,
event-driven program that does the same thing. OTOH, small servers
(only a few clients) often use one thread per connected socket for
programming convenience. Large servers cannot do this because the
number of threads becomes inefficient. More advanced techniques are
used (In Windows, thread pooling and IOCP).

Your topic has no relevance whatsoever to the standard C or C++
languages. Newsgroups trimmed.

-- 
Scott McPhillips [VC++ MVP]


Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... Client client = new Client; ...
    (microsoft.public.vc.language)
  • Re: TCP server stop receiving new connections
    ... reset the event mask of your listening socket each time you ... I have a strange problem in my class library used by all our client ... server applications. ... incomming connections, but keeps current connections. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)