Re: SetSockOpt with SO_REUSEADDR parameter



See below...
On Fri, 23 Mar 2007 05:16:36 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

When a server accepts a TCP connection, a new socket is created with a
"unused" port on server. So, every sockets will use different ports. Right???

Well, I think I was wrong on that one. Sorry for confusing you.
The accept call creates a new socket with the same local port number.
The difference is, that this socket is not in an unconnected (listening)
state.
Listening socket accepts a connection and creates a new socket with "the
same" local port number. So, the port number of listening socket is the same
as this new socket createed by accept call. If yes, why do these two sockets
use the same port?
****
TCP is not UDP. TCP is not UDP. TCP is not UDP. Do not confuse the two. They are
completely different in this regard.
****

There can only be one listening socket on any local port number.

The problem with UDP is that UDP does not have connections. So all
sockets you create and bind to a port are always listening sockets.
That's why you can only have one UDP socket per port number.
What you say is UDP is a connectionless style. It will always listen on a
port.
And because there can only be one listening socket on a port, I can't create
multiple UDP sockets with the same port.
****
THe concept of "listening socket" applies only to TCP. UDP sockets do not support the
concept of "Listen". A UDP socket merely receives a message from someplace. When you
send a message via UDP, you specify the IP and port for the recipient explicitly, because
there is no "connection"; this is what is meant by "connectionless protocol". So for
sending, you do not need "multiple ports" because the concept has no meaning, and a single
port suffices at all times.
****
If I try to do this, I will get an
error code "10048 Address/Port already in use". Right???
****
Yes. You are SUPPOSED to get that error, because the address/port pair indeed is already
in use.
****
MSDN tells me to use "setsockopt (SO_REUSEADDR)" :
============================================
For server applications that need to bind multiple sockets to the same port
number, consider using setsockopt (SO_REUSEADDR).
============================================
****
Yes, this will allow the socket/port pair to be reused. You do this because sometimes the
network stack thinks the address/port pair is still in use after the server program has
terminated, and would be used EXACTLY ONCE when you create the ONE AND ONLY UDP port that
your app will use. It makes no sense to keep doing this each time, because all you are
doing is effecitively CLOSING the one-and-only UDP port, then re-creating the one-and-only
UDP port, but with a different socket handle, rendering all other socket handles to this
one-and-only port as broken. You create a UDP port with a give port # exactly ONCE each
time your program starts, and ONLY ONCE each time your program starts. You will then pass
the handle to that one-and-only UDP port to all the threads that want to send data using
it.
*****
I use setsockopt with SO_REUSEADDR to overwrite the Create function. I seem
to solve the 10048 problem and the system is working. What I get from MSDN
is I can bind mutiple sockets to the same port number by using setsockopt
(SO_REUSEADDR). Is there something wrong?
****
Yes. You are solving a nonexistent problem with an erroneous solution, by trying to do
something that does not make sense. When the OS tells you that it does not make sense,
you are saying, in effect, "shut up and do it!". So it does it. It still doesn't make
sense, but it does it.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: SetSockOpt with SO_REUSEADDR parameter
    ... So I create multiple UDP sockets with the same port to send data. ... happening is that you are throwing away the old socket and replacing it with the new ... When the second client is connecting to server, I still get the error code ...
    (microsoft.public.vc.mfc)
  • Re: Problem with socket
    ... Be aware that those port numbers are part of the IANA-assigned range. ... socket operations on sockets for which there are no handles... ... The result of using comma lists is ... you have used the completely meaningless word "crash" to describe your ...
    (microsoft.public.vc.mfc)
  • Re: How to uniquely identify a UDP session at Winsock layer?
    ... For UDP, once a send is done, the socket will be auto-bound if it has not ... > Theoretically, I guess, a connection or session in uniquely identified ... > by "Source IP, Source port, Destination IP, Destination port". ...
    (microsoft.public.pocketpc.developer.networking)
  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: ISA Event
    ... applying ISA SP1 resolved the issue. ... So it's worth asking - is this SBS ... > Web Proxy service failed to bind its socket to 192.168.4.9 port 443. ...
    (microsoft.public.backoffice.smallbiz2000)