Re: SetSockOpt with SO_REUSEADDR parameter



See below...
On Wed, 21 Mar 2007 23:55:21 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

****
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.
****

I write a server application to send real-time video frame to client(s).
When a client logins into server, I will create a UDP socket to send
real-time video frame to it.
****
That is not the same as creating multiple sockets with the same port #. It is also not
clear why you need to be creating multiple sockets. But if you do create sockets, you
need to create them in the "transient port" area which is 49152 (0xC000) to 65536. You
seem to not understand that the concept of creating "multiple sockets with the same port
number" simply does not exist. So you are still working with a single port,
systematically destroying the one and only port and replacing it with a new single port of
the same port number.
*****
So, I think I will use multiple sockets with the same port to send frame.
Is this concept wrong?
****
Yes. Insofar as the Internet protocol is defined, you are making a statement that is
completely nonsensical. You *can't* create "multiple ports with the same port number".
This is not a concept that exists in the network support.
****

****
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!!!!
*****
Thanks for your suggestions. I will remember and change my bad programming
style.

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.
The reason I use mutiple UDP is the beginning of this response.
Because I will send real-time video frame, I use UDP socket.
I will re-describe my idea. The basic architecture is based on the source
[http://support.microsoft.com/kb/192570]
1) Server listen on port 9898
2) Client spawns a thread, which connects to the server
3) Server accepts the connection and spawns a thread to handle the socket
communication and send real-time frame
4) The thread creates a UDP socket with port 9899
5) The thread sends the real-time frame with UDP socket to client.
So, I think I need one UDP socket for one client.
*****
No, you took an article on a completely unrelated technology and are misapplying
everything in it. The article clearly is titled "Message-Oriented TCP and Multithreaded
Client/Server" and consequently has nothing to do with UDP. In TCP, an Accept call
creates a new TCP socket, which does NOT have the same port # as the listening socket. You
are taking a solution in a completely different problem domain, misinterpreting it, and
applying it in a context in which it cannot possibly work.

By the way, I have just read the code of that article, and it represents some of the most
egregiously poor examples of Windows multithreaded programming I have seen in a very long
time, anywhere. It represents worst-practice multithreading, and I would give it an F
grade for quality of multithreading. Microsoft should consider this example an
embarrassment. It was written by an amateur who once heard multithreading described over
the telephone. For example, it uses a single global buffer to pass information and a
global lock on the buffer, one of the worst possible choices to make for a multithreaded
server. And it is erroneous because it doesn't really handle multiple threads correctly
(the ::PeekMessage loop is a great example of how do a job wrong). It improperly
synchronizes the shared global variable so that any thread is free to overwrite it, so
information is lost. Overall, this has to rank as one of the WORST programming examples I
have ever seen on the MSDN. In addition, it defines user-defined messages with WM_
prefixes, defines project-specific constants in stdafx.h, duplicates definitions in
different files, and overall looks like a summer intern's first project. Someone who
didn't know MFC, didn't know threading, didn't know sockets, and on the whole could be
construed as totally clueless about basic good programming practice.

It is so deeply offensive that it may be my first venture to the MSDN Wiki: to rewrite
this code to represent rational multithreading practice. There is, at first glance,
nothing in the multithreading that could be construed as correct methodology.

(later) I just rewrote the server code. Not a single subroutine was left untouched. Most
were egregiously wrong and required a total rewrite.
****

After I change the following code:
==============================================================
void CUdpSendSocket::CreateSocket(UINT nPort)
{
CString szError;

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

if (bRet > 0) {
szError.Format(_T("Send Socket Create() failed: %d"), ::GetLastError());
AfxMessageBox(szError);
return;
}
m_nPortNum = nPort;

BOOL reuse = TRUE;
if (!SetSockOpt(SO_REUSEADDR ,&reuse, sizeof(int))) {
szError.Format(_T("SetSockOpt failed to set SO_REUSEADDR:%d"),
::GetLastError());
AfxMessageBox (szError);
}
}
==============================================================
It still can't work for create UDP sockets with the same port. What shall I
do?
****
Find some articles on how UDP sockets work and read them. What you are doing here is
essentially nonsensical. A good book on network programming (e.g., Comer & Stevens,
Volume III, Windows edition) would be a good investment.
****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: UDP source port number when using RAW socket??
    ... the source port in a UDP packet is an *optional* ... If you *do* expect UDP packets in reply to the packets you send out, ... then what you really want to do is open a plain UDP socket and bind ... Use your raw socket to send out packets and your UDP socket to receive ...
    (comp.os.linux.development.system)
  • Re: UDP socket with multiple ports
    ... the reason why I need to receive data from more than one UDP port is ... use UDP and often multiple when it's a Microsoft game. ... to listen to every port (IP raw mode) or just one? ... multiple sockets, socket per port... ...
    (microsoft.public.dotnet.framework)
  • Re: UDP socket with multiple ports
    ... Keep in mind that a Socket, by definition, represents a single unique ... to one port per Socket. ... RT> for network games to use UDP and often multiple when it's a Microsoft ... DirectX has DirectPlay technology that is used for network communication. ...
    (microsoft.public.dotnet.framework)
  • Re: SetSockOpt with SO_REUSEADDR parameter
    ... When a client logins into server, I will create a UDP socket to send real-time video frame to it. ... This endpoint is described by the local machine's IP address and a local port number. ... When you refer to "connect" here I assume you mean that the client calls a function named Connect. ...
    (microsoft.public.vc.mfc)
  • Re: SetSockOpt with SO_REUSEADDR parameter
    ... Why is it so important to you to use the SAME source port number for all clients? ... I simply do not believe your boss told you to use the same server socket and port number for all client streaming data. ... I want to add the UDP socket into this architecture to send real-time frames. ...
    (microsoft.public.vc.mfc)