Note that there are some explanatory texts on larger screens.

plurals
  1. POUniting TCP and UDP in C#
    text
    copied!<p>So ideally, I am looking for a way to unite TCP and UDP on the server and manage both these connections under individual client threads. Currently, I am wondering if it is possible to accept a TCP connection and set up UDP communication from that.</p> <p>Here is my ideal setup:</p> <ol> <li>Client connects on TCP to server via TCPClient.connect()</li> <li>Server accepts TCP connection via TCPListener</li> <li>when server accepts TCP connection, it also gets the IPEndpoint from the TCP connection</li> </ol> <p>and uses that to begin UDP communcation with:</p> <pre><code>serverUDPSocket.BeginReceiveFrom (byteData, 0, 1024, SocketFlags.None, ref (Endpoint)ThatIPEndpointThatIJustMentioned, new AsyncCallback(client.receiveUDP), (Endpoint)ThatIPEndpointThatIJustMentioned); </code></pre> <p>^so this is where I am running into a little bit of a theoretical issue. From my understanding, the endpoints for TCP and UDP would be formatted differently. How can I resolve this issue? I would like to avoid having the client connect to UDP on a separate thread and then uniting these threads under a single managing class.</p> <p>EDIT:</p> <p>Here is the code that I am trying to implement:</p> <pre><code>//Listening for TCP TcpClient newclient = listenTCP.AcceptTcpClient(); //Accept the client Client clientr = new Client(newclient); //Create a new Client class to manage the connection clientr.actionThread = new Thread(clientr.action); //This thread manages the data flow from the client via the TCP stream clientr.actionThread.Start(clientr); EndPoint endPoint = newclient.Client.RemoteEndPoint; //so this is the sketchy part. I am trying to get the endpoint from the TCP connection to set up a UDP "connection". I am unsure about the compatibility as UDP and TCP sockets are different. UDPSocket.BeginReceiveFrom(new byte[1024],0,1024, SocketFlags.None,ref endPoint, new AsyncCallback(clientr.receiveUDP), null); //the AsyncCallBack is like the manager thread for UDP (same as in TCP) clients.Add(clientr); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload