(part 41) Han from China answers your C questions
- From: Borked Pseudo Mailed <nobody@xxxxxxxxxxxxxxxxx>
- Date: Sun, 23 Nov 2008 15:34:19 -0700 (MST)
Unable to bind a 2nd time if recv receives all bytes sent
Andre said:
Hi All,
I have written these very simple server and client, but the server
always crashes at the 2nd loop when it tries to bind again, and issues
an "Address already in use" error message.
But if I change
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength,0) < 0)"
to
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength - 1,0) < 0)"
the server is able to bind again in every single loop, but off course
it doesn't receive the correct value sent by the client.
How can this issue be corrected?
Hey, buddy.
I see you've already found the ideal solution by implementing the code
changes recommended by comp.lang.c posters. In future, you may want to
post to both comp.lang.c and comp.unix.programmer, since not everyone
on comp.lang.c recognizes the ANSI/ISO C topicality restriction, and
these people may be more than happy to help you with your programming.
It's purely a matter of personal choice, though.
So you've gone for yanking out the bind() and listen() from the loop
rather than using setsockopt() and SO_REUSEADDR, which was the wrong
*first* solution proposed by Martien Verbruggen (the same fellow who,
ironically enough, had this to say about me: "Don't post to both.
Unix network programming clearly belongs on comp.unix.programmer,
not on comp.lang.c. Adding comp.lang.c only provides you with noise from
people who don't really know the subject well enough, but feel they
have to comment anyway.")
I, like Martien Verbruggen, am not a comp.lang.c network programming
expert, so take what follows with a grain of salt. Perhaps you can
add comp.unix.programmer to the header (as Martien implored you to)
so that you can get the second version of Martien Verbruggen over
there, who is the greatest network programmer who has ever lived.
We all have split identities on Usenet, you see.
Any good book on network programming (you can count them on one hand)
will teach you about the TIME_WAIT state and how the side of a TCP
connection that performs the active close is the one that enters
the TIME_WAIT state. (This may be both sides in the case of a
simultaneous close.) You'll also learn about the rationale for the
TIME_WAIT state. It's this state in the TCP connection transition
that is responsible for the EADDRINUSE error you were receiving.
Although calling setsockopt() with the SO_REUSEADDR option (as
Martien recommended) wasn't the *first* solution in your case, you
may still wish to use it in your server code if you want to be able
to terminate the server and restart it. In fact, get into the
habit of setting this option in all your TCP server code, unless
you discover some reason not to.
Yours,
Han from China
All my posts will have subjects containing the words "Han from
China" or "Han from China's". This is so people can killfile me
easily if they wish. There are 3 posting IDs that my gateways
randomly assign me: George Orwell, Nomen Nescio, and Borked
Pseudo Mailed. You can killfile those too if you wish. Other
than that, my posts have a highly consistent format as well.
Thank you and good luck with your C programming.
.
- Follow-Ups:
- Unable to bind a 2nd time if recv receives all bytes sent
- From: thomas . mertes
- Unable to bind a 2nd time if recv receives all bytes sent
- Prev by Date: Re: long offset
- Next by Date: Re: global variables initialization
- Previous by thread: convert string to size_t
- Next by thread: Unable to bind a 2nd time if recv receives all bytes sent
- Index(es):
Relevant Pages
|