none
Can’t receive data from UDP Sockect

    Question

  • Hello:

    I have a server listening on an UDP port. When the server
    receives the text “Init”, starts to send me arrays of data. I want to receive
    the data from a WP7.5.

    I work with the latest 7.1 SDK. I test is with the
    emulator and with the phone.

    I take a look to the network traffic with
    Wireshark, and I see the Init command sended to the server, and the response
    from the server, but I have a Timeout in the phone side.

    The code was copied from the “How to: Create and Use a UDP Socket Client
    Application for Windows Phone” article in the MSDN -> http://msdn.microsoft.com/en-us/library/hh202864.aspx

    This is the code:

            private Socket _socket = null;
            static ManualResetEvent _clientDone = new ManualResetEvent(false);
            private const int TIMEOUT_MILLISECONDS = 1000;
            private const int MAX_BUFFER_SIZE = 256;
    
    
    
            public byte[] SocketTest(string IP, int port)
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                string rSend = Send(IP, port, "Init");
                byte[] rRec = Receive(port);
                return rRec;
            }
    
    
            public string Send(string serverName, int portNumber, string data)
            {
                string response = "Operation Timeout";
    
                // We are re-using the _socket object that was initialized in the Connect method
                if (_socket != null)
                {
                    // Create SocketAsyncEventArgs context object
                    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
    
                    // Set properties on context object
                    socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber);
    
                    // Inline event handler for the Completed event.
                    // Note: This event handler was implemented inline in order to make this method self-contained.
                    socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                    {
                        response = e.SocketError.ToString();
    
                        // Unblock the UI thread
                        _clientDone.Set();
                    });
    
                    // Add the data to be sent into the buffer
                    byte[] payload = Encoding.UTF8.GetBytes(data);
                    socketEventArg.SetBuffer(payload, 0, payload.Length);
    
                    // Sets the state of the event to nonsignaled, causing threads to block
                    _clientDone.Reset();
    
                    // Make an asynchronous Send request over the socket
                    _socket.SendToAsync(socketEventArg);
    
                    // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
                    // If no response comes back within this time then proceed
                    _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
                }
                else
                {
                    response = "Socket is not initialized";
                }
    
                return response;
            }
    
    
            public byte[] Receive(int portNumber)
            {
                byte[] response = null;
    
                // We are receiving over an established socket connection
                if (_socket != null)
                {
                    // Create SocketAsyncEventArgs context object
                    SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                    socketEventArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, portNumber);
    
                    // Setup the buffer to receive the data
                    socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
    
                    // Inline event handler for the Completed event.
                    // Note: This even handler was implemented inline in order to make this method self-contained.
                    socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                    {
                        if (e.SocketError == SocketError.Success)
                        {
                            // Retrieve the data from the buffer
                            response = e.Buffer;
                        }
                        else
                        {
                            response = null;
                        }
    
                        _clientDone.Set();
                    });
    
                    // Sets the state of the event to nonsignaled, causing threads to block
                    _clientDone.Reset();
    
                    // Make an asynchronous Receive request over the socket
                    _socket.ReceiveFromAsync(socketEventArg);
    
                    // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
                    // If no response comes back within this time then proceed
                    _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
                }
                else
                {
                    response = null;
                }
    
                return response;
            }
    

     

    Can somebody help me?

    Thanks for your time!

    Juan Segura

    Saturday, October 01, 2011 6:54 AM

Answers

  • There are not a problem with the code, the problem is the server.

    I open a new post post with the question.

    Saturday, October 08, 2011 4:26 PM

All replies

  • There are not a problem with the code, the problem is the server.

    I open a new post post with the question.

    Saturday, October 08, 2011 4:26 PM
  • Can’t receive data from UDP Sockect. can you explain briefly how to solve it.?

    Thursday, March 01, 2012 7:28 AM
  • HI,

    How can you solve the  udp socket receives fails timeout exception or connectionreset exception.. kindly mail me  pprkarthik@gmail.com


    Monday, April 09, 2012 6:46 AM