Note that there are some explanatory texts on larger screens.

plurals
  1. POUDP Socket: An existing connection was forcibly closed by the remote host
    primarykey
    data
    text
    <p>I'm fairly new with UDP sockets with most of my socket experience being with TCP.</p> <p>I'm using C# to create a UDP socket, bind it and then continually listen for packets from an IP. However, when the client sends a packet, the socket throws an exception: "An existing connection was forcibly closed by the remote host".</p> <p>This is how I create the UDP socket:</p> <pre><code>Socket UDPSocket = new Socket(SocketType.Dgram, ProtocolType.Udp); //Create UDP socket UDPSocket.Bind(new IPEndPoint(IPAddress.Any, 1338)); //Bind port 1338 from any IP </code></pre> <p>After creating the socket, I asynchronously receive data:</p> <pre><code>UDPSocket.BeginReceiveFrom(newClient.bufferu, 0, AbstractServerClient.BUFFER_SIZE, 0, ref newClient.remoteEndpoint,new AsyncCallback(ReceiveCallbackUDP), newClient); </code></pre> <p><code>newClient.remoteEndpoint</code> an endpoint from a TCP socket, so the UDP is effectively receiving packets from the same IP that the TCP is connected to. Assume the rest of the paramaters are valid (They work fine on TCP).</p> <p>The <code>ReceiveCallbackUDP</code> method looks like this:</p> <pre><code>public void ReceiveCallbackUDP(IAsyncResult ar) { AbstractServerClient client = (AbstractServerClient)ar.AsyncState; int bytesRead = UDPSocket.EndReceive(ar); if (bytesRead &gt; 0) { Bitstream data = new Bitstream(client.bufferu, 12); int bytesProcessed = 12; while (bytesProcessed &lt; bytesRead) { ushort messageLength = data.ReadUInt16(); //Read the message length byte messageID = data.ReadByte(); //Read the message ID // Logger.Log("Received message ID: " + messageID.ToString() + " (Size: " + messageLength + " bytes)"); bytesProcessed += messageLength + sizeof(UInt16); client.GetPacketHandler(messageID).HandlePacket(data); //Process the message bytesProcessed += 12; data.Seek(bytesProcessed); //Seek to the next message } } UDPSocket.BeginReceiveFrom(client.bufferu, 0, AbstractServerClient.BUFFER_SIZE, 0, ref client.remoteEndpoint, new AsyncCallback(ReceiveCallbackUDP), client); } </code></pre> <p>This function calls <code>EndReceive()</code> on the socket to read the data, and processes it, and called <code>BeginReceiveFrom</code> to wait for more data (asynchronously). The exception is thrown on the line <code>int bytesRead = UDPSocket.EndReceive(ar);</code></p> <p>Why is this happening? To my understanding, UDP is a connectionless socket so the exception "An existing <strong>connection</strong> was closed" doesn't make sense. Am I doing something wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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