Re: Sockets programming
From: Scott McPhillips [MVP] (org-dot-mvps-at-scottmcp)
Date: 01/16/05
- Next message: Bonj: "Re: Sockets programming"
- Previous message: TanKC: "Re: Sockets programming"
- In reply to: Bonj: "Sockets programming"
- Next in thread: Bonj: "Re: Sockets programming"
- Reply: Bonj: "Re: Sockets programming"
- Messages sorted by: [ date ] [ thread ]
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]
- Next message: Bonj: "Re: Sockets programming"
- Previous message: TanKC: "Re: Sockets programming"
- In reply to: Bonj: "Sockets programming"
- Next in thread: Bonj: "Re: Sockets programming"
- Reply: Bonj: "Re: Sockets programming"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|