0

I am having an issue with Windows Mobile.

I was writing a code for recieving UDP data from a windows server Aplication (created by me), which broadcasts data, but, when the windows application executes, I am encoutering a terrible issue:

An invalid argument was supplied

when the socket ReceiveFromAsync() funciton works.

Can anyone help me please?

--- this is the code

public string Receive(int portNumber)
        {
            string response = "Operation Timeout";
            // We are receiving over an established socket connection
            registerSocket();
            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 = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                        response = response.Trim('\0');
                    }
                    else
                    {
                        response = e.SocketError.ToString();
                    }
                    _clientDone.Set();
                    _socket.ReceiveFromAsync(socketEventArg);
                });
                // 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);**   // here the issue arises An     //invalid argument was supplied
                // 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;
        }
4

1 回答 1

0

我找到了解决方案。

要接收 Udp 多播,您应该_socket.Bind(IPEndpoint)在接收之前调用并使用_socket.ReceiveAsync()而不是_socket.ReceiveFromAsync(). 也socketEventArg.RemoteEndpoint应该是空的。

这段代码对我有用:

        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
        args.SetBuffer(new byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
        args.Completed += args_Completed;

        _socket.Bind(new IPEndPoint(IPAddress.Any, **your port**));
        _socket.ReceiveAsync(args);
于 2014-07-03T14:20:04.613 回答