Re: SetSockOpt with SO_REUSEADDR parameter
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 22 Mar 2007 00:49:02 -0500
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?Joseph M. Newcomer [MVP]
What shall I do?
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: SetSockOpt with SO_REUSEADDR parameter
- From: mmlab_js
- Re: SetSockOpt with SO_REUSEADDR parameter
- Prev by Date: Re: error C2661: 'CStdStr<CT>::Format' : no overloaded function takes 19 argumentswith[CT=char]
- Next by Date: Re: SetSockOpt with SO_REUSEADDR parameter
- Previous by thread: Re: Increment version and build automatically
- Next by thread: Re: SetSockOpt with SO_REUSEADDR parameter
- Index(es):
Relevant Pages
|
|