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;
}