473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sockets programming

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).

Jul 22 '05 #1
11 2158
Bonj <a@b.com> scribbled the following
on comp.lang.c:
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.


Neither C or C++ has any socket, TCP, or other networking support
whatsoever. The fact that you are cross-posting to
microsoft.public.vc.language looks like you are using Microsoft
Windows. I advise therefore to ask on
comp.os.ms-windows.programmer.win32.
If, by any chance, you are using Unix, ask on comp.unix.programmer.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Jul 22 '05 #2
Yes.. You can have a server that listens at a desired port for
multiple-clients and communicates using that same port. If you are using
blocking socket, you will have to spawn a worker thread to handle each
client connection.

You probably want to take a look at the following article about blocking and
non-blocking socket :

http://www.developerfusion.co.uk/show/28/8/

TanKC
"Bonj" <a@b.com> wrote in message news:34*************@individual.net...
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).

Jul 22 '05 #3
Ta very much, appreciate it

"TanKC" <kc*****@google.com> wrote in message
news:O7****************@TK2MSFTNGP15.phx.gbl...
Yes.. You can have a server that listens at a desired port for
multiple-clients and communicates using that same port. If you are using
blocking socket, you will have to spawn a worker thread to handle each
client connection.

You probably want to take a look at the following article about blocking
and
non-blocking socket :

http://www.developerfusion.co.uk/show/28/8/

TanKC
"Bonj" <a@b.com> wrote in message news:34*************@individual.net...
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).


Jul 22 '05 #4

Off topic for comp.lang.*

However, I suggest you look at some C++ libraries for this sort of thing.

e.g. CommonC++ or ACE.

e.g.
http://www.gnu.org/software/commoncp...p-example.html

I wrote that a while back and I would do it very differently today.

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).

Jul 22 '05 #5
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 as
well as windows, (the only thing seemingly different in linux
seems to be that it has different includes and doesn't have to
call WSAStartup).


You are way off topic for c.l.c, and probably for c.l.c++ also,
both of which deal only with the portable ISO standardized
languages (which are not the same). The standard languages do not
contain sockets, multithreading, etc. You should read a newsgroup
for a while before posting into it. You should also set followups
to one group when initially posting a cross-posted inquiry.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 22 '05 #6
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:

Yes, more than one way exists to do what you're describing. You could
call listen() and fork on each accept(), or you could use the select()
system call. Forking on accept() generates overhead on process
creation/switching and is generally more cumbersome than using
select(). select() blocks and waits for input/output from user defined
file descriptors. When input/output arrives on one of these file
descriptors, select() returns the file descriptor in question, allowing
for efficient "multiplexing" of requests using only a single process.
As always, see select()'s man pages for more information.
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),...


Yes, call the setsockopt() function with the SO_REUSEADDR parameter
set.
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR,1)

-dave

Jul 22 '05 #7
Yes, more than one way exists to do what you're describing. You could
call listen() and fork on each accept(), or you could use the select()
system call. Forking on accept() generates overhead on process
creation/switching and is generally more cumbersome than using
select().


Honestly in modern UNIX systems fork()ing doesn't have that much
overhead. It used to be that the entire process space had to be copied
to the new process, but now the page tables are simply marked
copy-on-write, and no actual copying of pages takes place except when
changes occur.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 22 '05 #8

"Bonj" <a@b.com> дÈëÓʼþ news:34*************@individual.net...
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).


There are several ways to do that without multithreading.
You can use select in linux and WaitForMultipleObjects in windows.
Jul 22 '05 #9

"Bonj" <a@b.com> дÈëÓʼþ news:34*************@individual.net...
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).


There are several ways to do that without multithreading.
You can use select in linux and WaitForMultipleObjects in windows.
Jul 22 '05 #10
That may be true, but the true power of select() is seen when multiple
sockets are in use; testing FD_ISSET usually gets the job done quickly
for me. Implementations using select() for input polling usually
involve a very short loop no matter how many input sources exist, as
opposed to the monstrosities that can result from repeated usage of
accept()/fork(). select() also allows you to easily set a timeout value
on each socket. One problem worth mentioning about select(), though, is
that many sockets can be operating in the same process space. You must
be wary of shared resource usage when programming with select(),
whereas with fork(), no such precautions need to be taken. The question
you need to ask yourself before developing a socket based daemon is, as
it usually is, what is the intended use of your program? If it's going
to be servicing many concurrent requests and queueing requests is not
reasonable, using select() is probably the better solution. If not,
using accept()/fork() may prove to be easier.

-dave

Jul 22 '05 #11
dave windsor wrote:
That may be true, but the true power of select() is seen when multiple sockets are in use;

Please learn to quote properly on usenet. See the following for
assistance:
http://groups-beta.google.com/group/...9e82ab0843783d

Brian

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jubi | last post by:
I am trying to program for a PocketPC using C#. I know it is possible for sockets programming for a desktop using C#. I'd like to know if it's possible to: 1. Sockets programming for the...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
1
by: Sougato Das | last post by:
Does anyone know any good places for information on sockets programming using VB.NET. I know VB.NET fairly well, but I know very little about sockets. Thanks.
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
2
by: djc | last post by:
I am very new to this... I just started playing around with some network programming. I really only use vb.net. I say that so I don't get the 'use c/c++' answer. Programming is a hobby for me, not...
1
by: Farslayer | last post by:
I'm trying to find a good source for programming sockets with VB.NET. The examples in the MSDN help do not show how to accept incoming connections and add this connection to a local array or...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.