Re: SetSockOpt with SO_REUSEADDR parameter

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Is it a synchronous or asynchronous socket? There are two different approaches. One is
that you unwrap the socket and pass an HSOCKET across to all threads. Another, the one I
tend to use, is to create a worker thread that blocks on an I/O Completion Port, and have
exactly one thread that talks to the UDP socket, and each thread that wants to send
something queues it up to be sent by the one and only thread that owns the socket.
joe

On Fri, 23 Mar 2007 19:46:05 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

"Joseph M. Newcomer" wrote:

See below...
On Fri, 23 Mar 2007 02:14:03 -0700, mmlab_js <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

mmlab_js schrieb:

I override the CASyncSocket::Create to solve this problem.

Why is it so important to you to use the SAME source port number for all
clients? What is the *real* problem you are trying to solve? I simply do not
believe your boss told you to use the same server socket and port number for all
client streaming data. So what is the real issue?
I construct a streaming server that sending a real-time frame captured from
webcam to clients who login in. At first, because I don't know how to write a
server-client architecture, I reference
[http://support.microsoft.com/kb/192570].
*****
Which is a horrible example about how to build a TCP server, and which would not work for
building a UDP server
*****
At second, I want to add the UDP
socket into this architecture to send real-time frames. So, I add UDP socket
into CServerThread class and create it at
****
You would create exactly ONE such socket, probably around the time you did the Listen()
call for TCP connections, and use it in all your child threads. You would NOT do it in
the InitInstance of EACH server thread.
****
===============================================
BOOL CServerThread::InitInstance()
{
UINT dummy;
m_socket.GetPeerName(m_UdpSocket.m_strClientAddress, dummy);
m_UdpSocket.CreateSocket(DATA_PORT);
return TRUE;
}
===============================================
The client application create a UDP socket with the same port DATA_PORT and
it can get the real-time frame from server. That's my original idea.
****
But there is no concept of creating multiple sockets with the same port addresss. So this
is flat-out wrong.
****
Usually, when doing things liek this and I need one socket per "connection",
then I bind the local socket to PORT_ANY because I do not care. WHat is
important is that the destination port number is correct, usually not the source
port number.
****
You still aren't getting it. You can create AT MOST ONE socket with a given port number.
ONE. AT MOST.
****
When using TCP, the source port number is usually totally irrelevant, what
counts is the destination port when creating a connection. When a server accepts
a TCP connection, a new socket is created with a totally new and unrelated
server port number.

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???
****
For TCP, yes. But you are using UDP to send data, and therefore the issues about what TCP
does are irrelevant
*****

Why do you insist on using the same server port number for
all your UDP traffic when even TCP does not do it that way?
Do you really understand the concept of source and destination port number, the
difference between these two, and how TCP and UDP handle them?
The client can detect whether a received packet comes from the correct server by
looking at the source address in this case. If you really need to check the
source port, then tell the client the automatically generated port number over
the TCP control connection that the server seems to haver to the client.
I will re-study again.
****
You only need one socket. So create the one socket, once, and use it everywhere.
****
Ok~ I understand what the problem is.
But How can I pass a UDP socket to server thread?
I want every server thread has a UDP socket handle can sending data.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: network programming: how does s.accept() work?
    ... The articles and books I've read all claim that the server ... port 5053 is a 'listening' port only. ... the server creates a new socket for communication between the client ... on the network, and in the RFCs which define the TCP protocol (UDP too, but ...
    (comp.lang.python)
  • Re: Detecting when a socket has been closed.
    ... shutdown the server closing the socket the client still thinks the socket is open. ... First send will not throw exception, because there is no way for TCP stack to know that other end has actually closed its receiving side. ... It takes some time for packet to reach other side and return information that server side is gone. ... in case other end has closed socket and network is still operating after first send TCP RST segment will be returned to TCP stack notifying it connection has gone down. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [FC3] MySQL TCP/IP Config
    ... By default the Fedora Core setup of MySQL allows both UNIX socket ... connections on the host itself as well TCP socket connections through ... port 3306, either locally or remotely. ... MySQL server ...
    (Fedora)
  • Re: Definition of a socket on Suns website
    ... the server gets a new socket bound to a different port. ... It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. ...
    (comp.lang.java.programmer)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... overlapped sockets with an I/O Completion Port. ...
    (microsoft.public.vc.language)