* my server is blocking...
@ 2006-07-27 19:17 Norbert François
2006-07-27 19:37 ` Steve Graegert
2006-07-27 21:19 ` Glynn Clements
0 siblings, 2 replies; 3+ messages in thread
From: Norbert François @ 2006-07-27 19:17 UTC (permalink / raw)
To: linux-c-programming
Hi list,
I'm a beginner in C (sockets) programming, so please, excuse my stupid question.
I'm trying to do a servent (server+client) for a p2p connexion. When I
dissociate the client and the server, everything's all right. The
Server is waiting for (pending) a connexion and when I connect on it,
I've the behaviour I want.
But now, I tried to merge the server & client together. When I start
my program, the server is pending and the other part of my program
isn't executed :( (the server is pending in an "accept" state). I
tried to do some fork(), but it was useless (in fact, a new server is
restarted and I get a bind error). How can I solve this problem
(easily, if possible) ?
In advance, thank you for your help
norbert
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: my server is blocking...
2006-07-27 19:17 my server is blocking Norbert François
@ 2006-07-27 19:37 ` Steve Graegert
2006-07-27 21:19 ` Glynn Clements
1 sibling, 0 replies; 3+ messages in thread
From: Steve Graegert @ 2006-07-27 19:37 UTC (permalink / raw)
To: linux-c-programming
On 7/27/06, Norbert François <norbertlike@gmail.com> wrote:
> Hi list,
Norbert,
> I'm a beginner in C (sockets) programming, so please, excuse my stupid question.
>
> I'm trying to do a servent (server+client) for a p2p connexion. When I
> dissociate the client and the server, everything's all right. The
> Server is waiting for (pending) a connexion and when I connect on it,
> I've the behaviour I want.
>
> But now, I tried to merge the server & client together. When I start
> my program, the server is pending and the other part of my program
> isn't executed :( (the server is pending in an "accept" state). I
> tried to do some fork(), but it was useless (in fact, a new server is
> restarted and I get a bind error). How can I solve this problem
> (easily, if possible) ?
1. A process always blocks in a call to accept() waiting for socket
descriptors to be fetched from the queue of incomplete connection
requests. Usually, when accept() returns, another process is forked
or some thread created to serve the client.
2. The bind() error you're probably observing is the famous "address
already in use" thing. You can solve the issue by setting the
SO_REUSEADDR socket:
int on = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
with s being the socket descriptor to set the option for. The socket
option must be set before the call to bind().
There are some good documents on the Internet dealing design of
concurrent servers and I'd highly recommend to get your hands on W. R.
Stevens' classic "Unix Network Programming" which is considered to be
one of the most comprehensive guides on network programming with the
sockets API. It's worth every penny.
\Steve
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: my server is blocking...
2006-07-27 19:17 my server is blocking Norbert François
2006-07-27 19:37 ` Steve Graegert
@ 2006-07-27 21:19 ` Glynn Clements
1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2006-07-27 21:19 UTC (permalink / raw)
To: Norbert François; +Cc: linux-c-programming
Norbert François wrote:
> I'm a beginner in C (sockets) programming, so please, excuse my stupid question.
>
> I'm trying to do a servent (server+client) for a p2p connexion. When I
> dissociate the client and the server, everything's all right. The
> Server is waiting for (pending) a connexion and when I connect on it,
> I've the behaviour I want.
>
> But now, I tried to merge the server & client together. When I start
> my program, the server is pending and the other part of my program
> isn't executed :( (the server is pending in an "accept" state). I
> tried to do some fork(), but it was useless (in fact, a new server is
> restarted and I get a bind error). How can I solve this problem
> (easily, if possible) ?
You essentially have three choices:
1. Run the client and server in separate processes (i.e. fork()).
2. Run the client and server in separate threads.
3. Use select() or poll() to check that operations on sockets won't
block.
#1 is the easiest to implement, but it makes integrating the client
and server more awkward. #2 requires being able to write thread-safe
code, which can be quite complex.
#3 tends to be messy, as you have to allow for the fact that
read/write operations will often be incomplete, i.e. a read() may
return part of a request, then the next part may not arrive until
later, and you still need to service the other half (client/server) in
the meantime.
--
Glynn Clements <glynn@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-07-27 21:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-27 19:17 my server is blocking Norbert François
2006-07-27 19:37 ` Steve Graegert
2006-07-27 21:19 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).