Re: SetSockOpt with SO_REUSEADDR parameter



See below...
On Wed, 21 Mar 2007 19:44:03 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,
I write a server application to send message to multiple clients connected
to it. So I create multiple UDP sockets with the same port to send data.
****
This concept does not exist. So whatever you are trying to do, it won't work. There is
no concept that allows you to create multipe sockets with the same port number. What is
happening is that you are throwing away the old socket and replacing it with the new
socket.
****
At
first, I get the error code " 10048 " and find a solution from MSDN. It tells
me to use SetSockOpt with SO_REUSEADDR. I rewrite the function that creates a
UDP socket:
*****
Essentially, you got a legitimate error message, and you created a workaround that causes
the message to be ignored, but the error still exists, and using SO_REUSADDR is solving a
problem other than the one you have described.
*****
=======================================
// CUdpSendSocket is derived from CAsyncSocket
void CUdpSendSocket::CreateSocket(UINT nPort)
{
TCHAR szError[256];
****
Since you are using MFC, why are you declaring a fixed-size TCHAR array for formatting
data? Use a CString!
****

BOOL bRet = Create(nPort, SOCK_DGRAM, FD_WRITE);

if (bRet != TRUE) {
*****
NEVER, EVER, UNDER ANY CIRCUMSTANCES IMAGINABLE, EVER WRITE SOMETHING LIKE THIS!

THIS IS ALWAYS AN ERROR!!!

It make no sense at all to compare a boolean value to TRUE or FALSE; it is as pointless as
writing
if( (a > 0) == TRUE)
instead of writing
if(a > 0)
since a boolean value always stands for, well, a boolean value. Comparing a boolean value
to TRUE is ALWAYS an error, since ANY non-zero value is non-FALSE, but you have
erroneously insisted that it be equal to the literal TRUE!

LOSE THIS BAD PROGRAMMING STYLE IMMEDIATELY!!!!
*****
wsprintf(szError, "Send Socket Create() failed: %d", GetLastError());
****
And why are you using a dead API (wsprintf), using a non-Unicode-aware string, and using
an API noted for its ability to do a buffer overrun? You should write
CString msg;
msg.Format(_T("Send Socket Create() failed: %d"), ::GetLastError());
****
AfxMessageBox(szError);
return;
}
m_nPortNum = nPort;

int flag = -1;
****
Exactly what part of the documentation did you fail to read? It CLEARLY states that the
type of the SO_REUSEADDR option is a BOOL, yet you use an 'int'; if a BOOL, the values are
TRUE and FALSE, yet you use -1! You should have written
BOOL reuse = TRUE;
if(!SetSockOpt(SO_REUSEADDR, &reuse, sizeof(BOOL)))
****
if (!SetSockOpt(SO_REUSEADDR ,&flag, sizeof(int))) {
wsprintf(szError, "SetSockOpt failed to set SO_REUSEADDR:% d",
GetLastError());
AfxMessageBox (szError);
****
Again, forget you ever heard of wsprintf. This API call should be thought of as being an
obsolete API used only in 16-bit Windows, and NEVER used in MFC
****
}
}
=======================================
When a client is connecting to server, server will accept the connection and
create a thread that creates a CUdpSendSocket socket to send data.
When the second client is connecting to server, I still get the error code
"10048".
****
I'm not sure what you are doing here or why you are needing to reuse the address for
multiple connections, because UDP is a connectionless protocol.

For that matter, why are you using UDP? Its usability is questionable except in very rare
situations. You should assume that it can only be used within a single LAN. It can only
handle messages of about 512 bytes. It is a fundamentally unreliable protocol (no
delivery guarantees; any handler including the receiving computer is free to throw a UDP
message away at any time without any form of notification); it does not have any form of
flow control.

Presumably you have the IP address and port number of each of the clients, so you can just
send the message to them with the one-and-only UDP port you have. You do not need to have
multiple ports to do this.
joe

*****

Does I use SetSockOpt with wrong way?
What shall I do?
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
    ... The accept call creates a new socket with the same local port number. ... TCP is not UDP. ... And because there can only be one listening socket on a port, ...
    (microsoft.public.vc.mfc)
  • Re: Help - a possible bot
    ... What you are seeing with the incoming port 137 UDP requests is probably ... > 14 ACCESS,22:07:52,RuLaunch blocked from connecting to Internet ...
    (Incidents)
  • Re: Help - a possible bot
    ... Apparently traffic to port 137 is blocked - so maybe it's ... Also, when the Internet is on-line, the explorer and svchost ... What you are seeing with the incoming port 137 UDP requests is probably ... > 14 ACCESS,22:07:52,RuLaunch blocked from connecting to Internet ...
    (Incidents)
  • Re: Socket programming question.
    ... it assigns a new port number and provides the connecting ip address, ... the listen could be on port 80, but the link is random and could be on port ... I am new to socket programming and I have been searching on ... that controls the starting and stopping of listening to a socket. ...
    (microsoft.public.win32.programmer.networks)
  • 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)