(part 41) Han from China answers your C questions



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.

.



Relevant Pages

  • [UNIX] Hardening the BIND DNS Server
    ... Hardening the BIND DNS Server ... Your Domain Name Service is the road sign to your systems on the Internet. ...
    (Securiteam)
  • omniOrb server + java client
    ... virtual char* echoString; ... // Bind the context to root. ... I start this server and if I try to test with c++ client, ...
    (comp.object.corba)
  • MS DTC problem
    ... Server Driver]Distributed transaction error' ... Client side output: ... Source Port: 2940 ... Received Bind call from ...
    (microsoft.public.dotnet.distributed_apps)
  • MS DTC problem
    ... Server Driver]Distributed transaction error' ... Client side output: ... Source Port: 2940 ... Received Bind call from ...
    (microsoft.public.sqlserver)
  • Re: using Socket calls in OSdesign subproject
    ... You don't want to restrict your bind(). ... correctly, the client is trying to connect to the wrong IP or, if the client ... Is there any way to check which IP address the server binds to when i use ... It comes to the level of accpeting socket and blocks there. ...
    (microsoft.public.windowsce.app.development)