CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2007
    Posts
    1,546

    UdpClient.BeginReceive gives socket error 10054

    I'm using udp at the moment, and every now and again the UdpClient i'm using decides to throw a socket exception with error code 10054. Google tells me this:

    Socket error 10054 may be the result of the remote server or some other piece of network equipment forcibly closing or resetting the connection
    But this is UDP. There is no connection. What exactly is causing this, and how exactly can i avoid it? My temporary fix is to dispose the existing udpclient and instantiate another one, but that seems a bit stupid. The offending code is here:

    Code:
            private void EndReceive(IAsyncResult result)
            {
                try
                {
                    IPEndPoint e = new IPEndPoint(IPAddress.Any, endpoint.Port);
                    byte[] buffer = client.EndReceive(result, ref e);
    
                    // Fire the event to indicate i've received a message
                    if (MessageReceived != null)
                        MessageReceived(buffer, e);
                }
                catch
                {
                    // ignore
                }
                
                // This is the part which throws an exception randomly
                client.BeginReceive(EndReceive, null);
            }
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  2. #2
    Join Date
    Jun 2008
    Posts
    34

    Re: UdpClient.BeginReceive gives socket error 10054

    This isn't helpful, but why are you using UDP?

    Have you gotten similar errors with TCP?

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: UdpClient.BeginReceive gives socket error 10054

    Because that's what the specification calls for, I don't have a choice.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UdpClient.BeginReceive gives socket error 10054

    It may not mean the connection itself, but the binding of the UdpClient to the local endpoint. If the IPEndPoint is being destroyed by some unknown means, then the binding will be lost and errors like this may happen. Granted UDP is technically a connectionless protocol, but it still needs to bind to an end point using a socket in order to transmit data.

    My only guess is its probably network related, I use UDP in some of my apps and have encountered this problem before. My situation was due to losing Wi-Fi signal. Id check to make sure the connection isnt being lost intermittently and that something on the client side isnt forcing the IPEndPoint to become invalid.

    Quote Originally Posted by chase2534
    This isn't helpful, but why are you using UDP?

    Have you gotten similar errors with TCP?
    UDP is an excellent choice for sending data across a network that isnt really uber-important. We use it to send data string to a server from client applications, but the thing is, if we lose a data string or two, its not a big deal, another one will come in a second later.

    TCP/IP has a lot of overhead involved in programming with it, so its use is best kept to sending important data that is absolutely necessary for one thing or another. I do use it however in sending error reports and in the updater that updates our clients applications.

    Also, that's a rather interesting way of doing BeginReceive()/EndReceive(), never seen it done recursively, lol.
    Last edited by RaleTheBlade; September 17th, 2008 at 06:11 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: UdpClient.BeginReceive gives socket error 10054

    Quote Originally Posted by RaleTheBlade
    It may not mean the connection itself, but the binding of the UdpClient to the local endpoint. If the IPEndPoint is being destroyed by some unknown means, then the binding will be lost and errors like this may happen. Granted UDP is technically a connectionless protocol, but it still needs to bind to an end point using a socket in order to transmit data.
    Ah, interesting. I never thought of that, but I'd be a little dubious as to whether that's really the cause or not. I'm binding to 0.0.0.0 port 15000. This shouldn't be affected if a network card goes down. Then again, who knows, windows works in mysterious ways

    Also, that's a rather interesting way of doing BeginReceive()/EndReceive(), never seen it done recursively, lol.
    I thought that was the way everyone used BeginXXX/EndXXX. It's the pattern I use all the time anyway I wouldn't call it recursive (as stackdepth doesn't increase), it's closer to being serial. As soon as one connection is processed it loops around and waits for the next connection. Whatever you want to call it, it's still a nice way to write it
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UdpClient.BeginReceive gives socket error 10054

    Quote Originally Posted by Mutant_Fruit
    I thought that was the way everyone used BeginXXX/EndXXX. It's the pattern I use all the time anyway I wouldn't call it recursive (as stackdepth doesn't increase), it's closer to being serial. As soon as one connection is processed it loops around and waits for the next connection. Whatever you want to call it, it's still a nice way to write it
    I always just used Thread objects, I prefer to have direct control over what takes place during the async operation, hahaha. Plus... when in my C++ Data Structures course a few years back, the way we had to use recursion was nightmarish, lol.

    As for your recurring problem, Im not sure what else to say. There may be something weird going on in BeginReceive() thats causing those errors, Ive never had much luck with the async methods that .NET provides. Have you tried putting the UdpClient.Receive() on an actual Thread object and just using a while loop to continually receive and report data? That might help...

    It works basically the same way:

    Code:
    UdpClient.Receive(ref IPEndPoint);
    You just need to but it on a Thread because it is a blocking call. You might try that just for sh*ts and giggles to see if it works.
    Last edited by RaleTheBlade; September 17th, 2008 at 09:34 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: UdpClient.BeginReceive gives socket error 10054

    Quote Originally Posted by RaleTheBlade
    I always just used Thread objects, I prefer to have direct control over what takes place during the async operation, hahaha.
    But then you lose out on all the benefits of IOCP (io completion ports). If you're using a lot of sockets, you really should use the async methods as opposed to a dedicated thread.

    The issue still occurs with a dedicated thread and blocking calls to receive. Maybe my 'solution' is the best way to fix it. The only problem is that even if i instantiate a new listener, how do i know that it won't blow up as soon as i call BeginReceive. That's my problem, i don't know when it's safe and when it's not.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: UdpClient.BeginReceive gives socket error 10054

    That's so weird, I'm assuming you've already made sure that its not being blocked, or that its not being used by some other app. But something is closing that socket... What though? Does it happen on just that machine? Or does is it indifferent to the machine that it runs on? Maybe there's some app on the local machine that's chucking the client off of the IPEndPoint.

    Is there an inner exception to the SocketException that's being thrown?
    Last edited by RaleTheBlade; September 18th, 2008 at 02:06 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured