Sending data over UDP to a destination with multiple clients open - c#

I am currently using this function to send data from the server to the clients
private static void send_message(string ip, string message)
{
byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);
int port = 11000;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(packetData, ep);
}
But this would mean a destination IP/port can only have one client open to receive the data, because having two clients open would mean one client can retrieve data that was meant for another (if I'm correct).. how do I solve this?
Receiving function:
private static Int32 port = 11000;
private static UdpClient udpClient = new UdpClient(port);
public static void receive_threaded()
{
Thread t = new Thread(() =>
{
while (true)
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] content = udpClient.Receive(ref remoteIPEndPoint);
if (content.Length > 0)
{
string message = Encoding.UTF8.GetString(content);
parseMessage(message);
}
}
});
t.Start();
}

1) You should implement some kind of protocol so that your server has a "well known" port to accept connections. Use this port to inform your client ANOTHER port where the client must connect. Use a different port for each client.
Your client conects to the server at 11000. Your server assigns a unique port for the client, let's say 11001 for the firts client. Then the server opens a connection at 11001. The client closes connection at 11000 and opens a new connection at 11001 to receive the data.
2) Why UDP?

I don't see why you need to open a new socket at all. You already have each client's address and port, from the first packet they sent you. Just send a packet to that address:port. I absolutely don't get the other suggestion of setting up extra ports either.

Related

Hole Punshing __ Sending to local loop works but sending to external ip not

I spend hours trying to implement Hole punching in C# , my project need a connection to be made directly between me ( server ) and other slaves (peers) , I have third party server that I'm using to keep track external IPs which belongs to the server and peers .
the Code I managed to write until now works fine when I supply an internal IP for the server ( loopback or (0.0.0.0)) but it don't when I put my external ip .. it give no exceptions , but when using a tool like packet sender to test it , the server recieve the datagram just from the loopback , not my external .
here my Server implementation :
class Program
{
static byte[] dataToSend = Encoding.ASCII.GetBytes("Test");
// get the ip and port number where the client will be listening on
static IPEndPoint GetClientInfo()
{
// wait for client to send data
using (UdpClient listener = new UdpClient(11000))
{
IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("188.247.78.18") , 11000);
byte[] receive_byte_array = listener.Receive(ref groupEP);
return groupEP;
}
}
static void Main(string[] args)
{
var info = GetClientInfo(); // get client info
/* NOW THAT WE HAVE THE INFO FROM THE CLIENT WE ARE GONG TO SEND
DATA TO IT FROM SCRATCH!. NOTE THE CLIENT IS BEHIND A NAT AND
WE WILL STILL BE ABLE TO SEND PACKAGES TO IT
*/
// create a new client. this client will be created on a
// different computer when I do readl udp punch holing
UdpClient newClient = ConstructUdpClient(info);
Console.WriteLine(info.Address.ToString());
// send data
newClient.Send(dataToSend, dataToSend.Length);
Console.ReadKey();
}
// Construct a socket with the info received from the client
static UdpClient ConstructUdpClient(IPEndPoint clientInfo)
{
var ip = clientInfo.Address.ToString();
var port = clientInfo.Port;
// this is the part I was missing!!!!
// the local end point must match. this should be the ip this computer is listening on
// and also the port
UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 11000));
// lastly we are missing to set the end points. (ip and port client is listening on)
// the connect method sets the remote endpoints
client.Connect(ip, port);
return client;
}
}
and this is my Client implementation :
class Program
{
static void Main(string[] args)
{
string ipOfServer = "188.247.78.18"; //this is my external Ip and it doesn't work giving me nothing in the server Console ( but if I replaced it with 127.0.0.1 server console response)
int portServerIsListeningOn = 11000;
// send data to server
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress send_to_address = IPAddress.Parse(ipOfServer);
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, portServerIsListeningOn);
sending_socket.SendTo(Encoding.ASCII.GetBytes("Test"), sending_end_point);
// get info
var port = sending_socket.LocalEndPoint.ToString().Split(':')[1];
// now wait for server to send data back
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(port));
byte[] buffer = new byte[1024];
sending_socket.Receive(buffer); // <----- we can receive data now!!!!!
}
}
how to make this works , and how to test it to make sure that the server working in my pc from outside ?

UDP socket listener not receiving data

My local ip is : 192.168.0.70,
External ip is : 192.168.0.50 : 60000
I want receive data from external ip sending to local ip. I use Socket class because i can send data using remote IPEndPoint. But when Udp connection closed, local ip's port dynamically changing. How can i receive data?
private static void UdpConnect()
{
try
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.0.50"), 60000);
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
client.Connect(remoteEP);
byte[] sendbuffer = { 1, 2, 3 };
client.Send(sendbuffer);
byte[] receivebuffer = new byte[512];
client.Receive(receivebuffer);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
If you need to listen on a specific local port, use Socket.Bind and then Socket.ReceiveFrom.
You don't need the Socket.Connect call since UDP is a connectionless protocol.

C# UDP socket app to listen to messages from various sockets

I have a system monitor app that needs to listen to messages from various UDP sockets on another machine. The other sockets continuously send heartbeats to this given IP/port.
This exception gets thrown when calling BeginReceiveFrom:
"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"
I shouldn't have to call connect because data is already getting sent to this ip endpoint. plus the data is coming from various sockets.
private Socket m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// bind socket
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
m_localEndPoint = new IPEndPoint(ipAddress, 19018);
m_socket.Bind(m_localEndPoint);
m_socket.BeginReceiveFrom(m_data,
m_nBytes,
MAX_READ_SIZE,
SocketFlags.None,
ref m_localEndPoint,
new AsyncCallback(OnReceive),
null);
}
private void OnReceive(IAsyncResult ar)
{
int nRead = m_socket.EndReceiveFrom(ar, ref m_localEndPoint);
}
I was able to get the desired behavior with the UdpClient class.
Your problem was you were binding to the IP assigned to you, you should bind to IP you want receive from - in your case:
m_socket.Bind(new IPEndPoint(IPAddress.Any, 12345));

Knowing which UDP client is which

Is it possible to identify an earlier UDP client without keeping the socket open? I want to link an integer ID to each unique client, but I don't want to keep any additional threads open.
//Receive (Server)
private static Int32 port = 11000;
private static UdpClient udpClient = new UdpClient(port);
public static void receive_threaded()
{
Thread t = new Thread(() =>
{
while (true)
{
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] content = udpClient.Receive(ref remoteIPEndPoint);
if (content.Length > 0)
{
string message = Encoding.UTF8.GetString(content);
if (action_message_receive != null) action_message_receive(String.Format("Recv({0}): {1}", remoteIPEndPoint.Port, message));
parseMessage(message);
}
}
});
t.Start();
}
//Send (Client)
private static void send_message(string ip, string message)
{
byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);
int port = 11000;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(packetData, ep);
if (action_message_send != null) action_message_send("Send: " + message);
}
A client can request a (temporary) userID from the server, and the server will add it to it's database and notify the client when it's done doing that. However, I can't have the client send it's own userID when it's making requests because any memory altering application would mean a 'hacker' could gain access to other user's stuff.
Since the socket doesn't stay open, the IPEndPoint.Port changes every time the client sends something to the server, so I can't keep track of it with that. I could get it done by creating a username/pass on a userID request and having those sent on every single request involving the userID thereafter, but that would be silly.
So is there any way to do this without keeping a thread open for each client? I'm probably doing something really weird here because UDP is supposed to be a one way street, but I'm here to learn so I just had to ask.
You will need to introduce some sort of unique identifier chosen by the server and sent to the client for it to "behave and send it back for identification purposes". A random long integer should suffice.
UDP has neither connections nor identification/authentication mechanisms. If you want those, use TCP (but those can be bruteforced also...)

How to specify source port of a UdpPacket?

I wanted to send UdpPacket to a specific remote host (I already know the public IP and Port).
I wanted to use C#'s UdpClient class.
static int Main()
{
UdpClient client = new UdpClient();
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 9999);
byte[] data = GetData();
client.Send(data, data.Length, remoteEP);
}
When sending a packet, the UdpClient choose an available port automatically. I want to manually set the port, from which I send the packets.
Thanks for your help in advance!
Try specifying the endpoint when you create the UdpClient:
UdpClient client = new UdpClient(localEndpoint);
EDIT: Note that you can also specify just the port number:
UdpClient client = new UdpClient(localPort);
That may be somewhat simpler :)

Categories