none
SocketException: An existing connection was forcibly closed by the remote host RRS feed

  • Question

  • Hi!
    I got this error while using a UdpClient to Receive() data in a blocking connection.

        SocketException: An existing connection was forcibly closed by the remote host

    I ran a sniffer on the 2 computers that are communicating - both send 4 packets and receive 2 packets from each other.  I don't know who the "remote host" is that closed the connection.
    Any ideas would be appreciated!

    Mechi

    Here's the code: (I highlighted the row of the error)

    bool ConnectToRemoteVeg(byte[] receiveBytes)
    {

        UInt32 intIPaddr = BitConverter.ToUInt32(receiveBytes, 9);
        
    IPAddress peerIPaddr = new IPAddress(intIPaddr);
        
    UInt16 peerIPport = BitConverter.ToUInt16(receiveBytes, 13);
        intIPaddr =
    BitConverter.ToUInt32(receiveBytes, 15);
        
    IPAddress peerLocalIPaddr = new IPAddress(intIPaddr);
        
    UInt16 peerLocalIPport = BitConverter.ToUInt16(receiveBytes, 19);

        //build and send the 0x23 messages to all IPaddr + ports and wait for any response
     
       byte[] buffer = new byte[13];
        cpMakeMessage(0x23, 0,
    ref buffer);

        udpClient.Connect(peerIPaddr.ToString(), peerIPport);
        udpClient.Send(buffer, 6);
        PutMessageOnForm(
    "msg sent to " + peerIPaddr.ToString() + " on port " + peerIPport.ToString());

        udpClient.Connect(peerIPaddr.ToString(), peerIPport + 1);
        udpClient.Send(buffer, 6);
        PutMessageOnForm(
    "msg sent to " + peerIPaddr.ToString() + " on port " + (peerIPport+1).ToString());

        if (peerLocalIPaddr.ToString() != "0.0.0.0")
        {
            udpClient.Connect(peerLocalIPaddr.ToString(), peerLocalIPport);
            udpClient.Send(buffer, 6);
            PutMessageOnForm(
    "msg sent to " + peerLocalIPaddr.ToString() + " on port " + (peerLocalIPport).ToString());

            udpClient.Connect(peerLocalIPaddr.ToString(), peerLocalIPport + 1);
            udpClient.Send(buffer, 6);
            PutMessageOnForm(
    "msg sent to " +peerLocalIPaddr.ToString() +" on port " +(peerLocalIPport + 1).ToString());
        }

        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        udpClient.Receive(
    ref RemoteIpEndPoint);     // This is where I get the error messsage

        // Use the IPEndPoint object to determine which of these two hosts responded.
        // send msg to first response
        udpClient.Connect(RemoteIpEndPoint);
        udpClient.Send(buffer, 6);

        return true;
    }


    Mechi
    • Edited by MechiF Wednesday, July 2, 2008 1:57 PM has code
    • Moved by jack 321 Friday, July 4, 2008 7:24 AM off topic
    Wednesday, July 2, 2008 1:56 PM

Answers

All replies