UDP comm problem when switching from Sync to Async - c#

I have a simple C#/.NET 3.5 client/server apps I am trying to write to get a handle on how to communicate in UDP. I am able to get the sync methods working and sending data back and forth, but now I am trying to switch to the async methods and I keep getting the error, "The requested address is not valid in it's context."
My Client code is as follows:
IPEndPoint ipep = new IPEndPoint(ipaddr, ipport);
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint remote1 = (EndPoint)ipep;
AddMessage("Looking for connections");
byte[] strt = Encoding.ASCII.GetBytes("open");
newsock.SendTo(strt,remote1);//.BeginSendTo(strt, 0, strt.Length, SocketFlags.None, remote1, new AsyncCallback(OnSend), null);
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
newsock.ReceiveFrom(rcv, ref epSender);
My Server is more complicated, as I have already switched to Async communication.
In a worker thread I call:
try
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)sender;
AddMessage("Beginning Wait for connections");
newsock.BeginReceiveFrom(rcv, 0, rcv.Length, SocketFlags.None, ref remote, new AsyncCallback(OnReceive), null);
while (true)
{
if (pleasestop.WaitOne(20))
break;
ts = DateTime.Now - lastnoop;
if (senddata)
{
//...fill sending byte array...
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,0);//ipaddr, ipport);
EndPoint remote1 = (EndPoint)ipep;
newsock.BeginSendTo(sending, 0, sending.Length, SocketFlags.None, remote1, new AsyncCallback(OnSend), null);
}
}
In my OnSend callback function is where I get the error: "The requested address is not valid in it's context."
private void OnSend(IAsyncResult ar)
{
try
{
newsock.EndSendTo(ar);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Server Send Error");
}
}

I dont think the object state can be null, try this:
newsock.BeginSendTo(sending, 0, sending.Length, SocketFlags.None,
remote1, new AsyncCallback(OnSend), newsock);

I found my problem. I wasn't using the correct EndPoint in my server to send a response back to my client.
In my receive callbackup function I now use mySender, which is a SendPoint defined at the class level. I then use this to send a response back to my client.
newsock.EndReceiveFrom(ar, ref mySender);
newsock.BeginSendTo(sending, 0, sending.Length, SocketFlags.None, mySender, new AsyncCallback(OnSend), mySender);
My app is now working correctly.

Related

BeginReceiveFrom() is null?

I have been tring to set up a UDP client/server in windows .net sockets.
In the client script below I am able to send a message to the server when the client "connects", but I get a null reference when I try to call the BeginToReceeve() function.
I am confused as to why I can send a message (using the server endpoint of course), but not receive because of a null error. I am new to sockets and am wondering why this is not working?
The server is a running in a windows c# application, and the client is running in a c# script in Unity3D.
private EndPoint epServer;
private Socket clientSocket;
private byte[] myBuffer = new byte[1024];
private void ConnectToServer ( ) {
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint server = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 61456);//Connect to the Local server
epServer = (EndPoint)server; //Access the server
WritePacketAndSend("Hello Server");//This creates the packet and uses the BeginToSend() function to send the string to the server
try{
clientSocket.BeginReceiveFrom(myBuffer, 0, myBuffer.Length, SocketFlags.None, ref epServer, new AsyncCallback(ReceiveData), epServer);//<<< Null reference here not sure why...
} catch(Exception ex) {
print(ex.Message); //Keep getting: Object reference not set to an instance of an object
}
}
private void ReceiveData(IAsyncResult ar){
print("Entered ReceiveData");//The code never enters here
try{
IPEndPoint server = new IPEndPoint(IPAddress.Parse("127.0.0.1", 61456);
epServer = (EndPoint)server;
clientSocket.EndReceiveFrom(ar, ref epServer);
clientSocket.EndReceive(ar);
print(Encoding.UTF8.GetBytes(myBuffer));
clientSocket.BeginReceiveFrom(myBuffer, 0, myBuffer.Length, SocketFlags.None, ref epServer, new AsyncCallback(ReceiveData), epServer,
} catch (Exception ex){
print("Receive Data: " + ex.Message);
}
}

Sending UDP Broadcast for populating USR-TCP232 LAN Modules information

I am using USR-TCP232 LAN Modules for an embedded project.
I have a tool to query those modules made by the manufacturer, it works great.
But, I want it in my code in C#. So, I decided to make one for myself. I believe I am so close but I guess a small glitch which gives me hard times and I need somebody to put some shed light on.
I can send a UDP broadcast and I can observe the traffic via "WireShark".
It is quite similar with the original tool. But, I can't receive the answered data sent by the devices on the network in my code.
Console application in C#
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
namespace UDPer
{
class Program
{
static void Main(string[] args)
{
UDPer udp = new UDPer();
udp.Starter();
ConsoleKeyInfo cki;
do
{
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
switch (cki.KeyChar)
{
case 's':
udp.Send("0123456789012345678901234567890123456789");
break;
case 'x':
udp.Stop();
return;
}
}
Thread.Sleep(10);
} while (true);
}
}
class UDPer
{
private Socket udpSock;
private byte[] buffer;
public void Starter()
{
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, 0));
udpSock.EnableBroadcast = true;
buffer = new byte[1024];
udpSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//udpSock.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.
//The socket must not be bound or connected.
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
public void Stop()
{
try
{
udpSock.Close();
Console.WriteLine("Stopped listening");
}
catch { /* don't care */ }
}
private void DoReceiveFrom(IAsyncResult iar)
{
try
{
//Get the received message.
Socket recvSock = (Socket)iar.AsyncState;
recvSock.EnableBroadcast = true;
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
byte[] localMsg = new byte[msgLen];
Array.Copy(buffer, localMsg, msgLen);
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
//Handle the received message
Console.WriteLine("Recieved {0} bytes from {1}:{2}",
msgLen,
((IPEndPoint)clientEP).Address,
((IPEndPoint)clientEP).Port);
//Do other, more interesting, things with the received message.
string message = Encoding.ASCII.GetString(localMsg);
Console.WriteLine("Message: {0} ", message);
}
catch (ObjectDisposedException)
{
//expected termination exception on a closed socket.
}
}
public void Send(string message)
{
UdpClient udp = new UdpClient(1500);
var ipEndPoint = new IPEndPoint(IPAddress.Broadcast, 1500);
byte[] data = Encoding.ASCII.GetBytes(message);
udp.Send(data, data.Length, ipEndPoint);
udp.Close();
}
}
}
Wireshark capture
Note that: This Wireshark capture is exactly the same as the original tool does.
Some definitions:
My PC where C# application resides: 192.168.65.82
LAN Module IP Address: 192.168.65.8
Ports must be 1500 which is all OK.
Send payload for querying LAN Modules;
"0123456789012345678901234567890123456789"
So, I have tried tons of different options one by one but no jo so far.
What am I missing here?
A socket always has a dedicated port. Passing an IPEndPoint with port number 0 doesn't mean that you receive packets send to any port. Instead you are binding to a port assigned by the underlying service provider.
udpSock.Bind(new IPEndPoint(IPAddress.Any, 0));
If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000
https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind(v=vs.110).aspx
You need to use
udpSock.Bind(new IPEndPoint(IPAddress.Any, 1500));

Socket Server to handle multiple clients request concurrently

I want to handle multiple client's request concurrently using Socket Server.
i have 10 clients, which can send request to my server at same time.
How could i handle this?
Is it possible?
With below code, i can handle only one request at a time, after completion of the request, server accepts next request.
Due to this 10th client need to wait until 9 requests get complete.
Any help would be appreciated
Below is the code i am using for Server Socket
Socket _serverSocket;
byte[] _buffer = new byte[255];
void SetupServer()
{
try
{
Console.WriteLine ("Setting up server...");
IPEndPoint ipEndPoint = new IPEndPoint (IPAddress.Parse(_ip), _port);
// create server socket to listen new client's connection
_serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.Bind (ipEndPoint);
_serverSocket.Listen (100);
_serverSocket.BeginAccept (new AsyncCallback (AcceptCallback), null);
}
catch (System.Exception ex)
{
Console.WriteLine ("Server failed :: {0}", ex.Message);
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket clientSocket = _serverSocket.EndAccept(ar);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallback), clientSocket);
}
void RecieveCallback(IAsyncResult ar)
{
Socket clientSocket = (Socket)ar.AsyncState;
int received = clientSocket.EndReceive(ar);
// check if client is disconnected?
if (received == 0)
{
Console.WriteLine("client is disconnected...");
return;
}
// do whatever you want with received data here...
byte[] dataBuf = new byte[received];
Array.Copy(_buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
Console.WriteLine("Received Data: {0}", text);
// accept new request
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallback), clientSocket);
}
You should familiarize yourself with the concept of a "multi threaded server". You should start a thread for each client which connects to the server. So you can simultaneously handle multiple clients. Or you could use Tasks Parallel Library if you want to try newer .NET features.
But there are many examples out there in the web that describe how to implement such a server.

Send data over the network C#

I try to send a string over the network, this is my code:
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25);
TcpClient client = new TcpClient(serverEndPoint);
Socket socket = client.Client;
byte[] data = Encoding.ASCII.GetBytes(response);
socket.Send(data, data.Length, SocketFlags.None);
socket.Close();
client.Close();
When I run it I got System.Net.Sockets.SocketException
If you are using a connectionless protocol, you must call Connect before calling Send, or Send will throw a SocketException. If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.
Refer Socket.Send Method (Byte[], Int32, SocketFlags)
Assuming you are using a connectionless protocol the code should be like this,
string response = "Hello";
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
if (ipAddress != null)
{
IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, 25);
byte[] receiveBuffer = new byte[100];
try
{
using (TcpClient client = new TcpClient(serverEndPoint))
{
using (Socket socket = client.Client)
{
socket.Connect(serverEndPoint);
byte[] data = Encoding.ASCII.GetBytes(response);
socket.Send(data, data.Length, SocketFlags.None);
socket.Receive(receiveBuffer);
Console.WriteLine(Encoding.ASCII.GetString(receiveBuffer));
}
}
}
catch (SocketException socketException)
{
Console.WriteLine("Socket Exception : ", socketException.Message);
throw;
}
}
Next time, try including the exception message to explain what actually went wrong.

UDP client in C#

Am trying to make a simple UDP application using C sharp,nothing sophisticated,connect,send some text,and receive it! but it keeps throwing this exception!
"An existing connection was forcibly closed by the remote host"!
The code :
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tmpRemote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
Console.WriteLine("Stopping client");
server.Close();
thanks =)
You should tell the system that you are listening for UDP packets on port 9050 before you call Receive.
Add server.Bind(ipep); after Socket server = new Socket(...);
Have you tried checking that the IP address is valid and the port is not being used for something else?
Windows:
Start > Run > "cmd" > "ipconfig".
Try turning off your firewall software.
If you do not know the IP of the answering server, you better do:
recv = server.Receive(data);
Here is my suggetion to your code. You can use a do-while loop using a condition (in my example it is an infinite loop):
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.ReceiveTimeout = 10000; //1second timeout
int rslt = server.SendTo(data, data.Length, SocketFlags.None, ipep);
data = new byte[1024];
int recv = 0;
do
{
try
{
Console.WriteLine("Start time: " + DateTime.Now.ToString());
recv = server.Receive(data); //the code will be stoped hier untill the time out is passed
}
catch { }
} while (true); //carefoul! infinite loop!
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
Console.WriteLine("Stopping client");
server.Close();

Categories