Re: SetSockOpt with SO_REUSEADDR parameter
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 23 Mar 2007 12:03:32 -0500
See below...
On Fri, 23 Mar 2007 05:16:36 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
****Listening socket accepts a connection and creates a new socket with "theWhen 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.
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.What you say is UDP is a connectionless style. It will always listen on a
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.
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
.
- References:
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: Joseph M . Newcomer
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: mmlab_js
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: Norbert Unterberg
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: Norbert Unterberg
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: Norbert Unterberg
- Re: SetSockOpt with SO_REUSEADDR parameter
- Prev by Date: Re: CEditCtrl with automatic line feed
- Next by Date: Re: OnKeyDown not called for VK_DOWN or VK_RIGHT but is called for others.
- Previous by thread: Re: SetSockOpt with SO_REUSEADDR parameter
- Next by thread: Re: SetSockOpt with SO_REUSEADDR parameter
- Index(es):
Relevant Pages
|