How to specify source port of a UdpPacket? - c#

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 :)

Related

How can i get a remote endpoint at UDPClient?

I'm making a server app, and i need to know, from which address message has come to the server. At Socket class has a RemoteEndPoint to get know where message come from. Is it have some solution to find where message come from at UDPClient class? I searched web, but didn't find something about it.
As you have not posted any code, this is a simplified example, that should get the job done:
UdpClient udp = new UdpClient(5050);
private void Listen()
{
while (true)
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] recData = udp.Receive(ref anyIP);
string ip = anyIP.Address.ToString() //this is client IP address
}
}

Send a UDP message to another network using socket

I've been making a sample program wherein the user can broadcast messages using sockets and UDP connection. It was successful in LAN but I can't broadcast my messages to other networks (e.g. 10.15.1.11's message to 10.11.1.23). Here's my sample code:
Listener:
bworker = sender as BackgroundWorker;
Socket _ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint _ListenerEndPoint = new IPEndPoint(IPAddress.Any, _port);
_ListenerSocket.EnableBroadcast = true;
_ListenerSocket.Bind(_ListenerEndPoint);
//_ListenerSocket.Connect(MulticastIP, _port);
_ListenerSocket.Ttl = 255;
_ListenerSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastIP));
while (true)
{
byte[] msg = new byte[1024];
_ListenerSocket.Receive(msg);
string StringData = Encoding.Unicode.GetString(msg);
bworker.ReportProgress(0, StringData);
}
Sender:
Socket _ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_ClientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastIP));
_ClientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, int.Parse(ttl));
IPEndPoint _ClientEndPoint = new IPEndPoint(MulticastIP, _port);
_ClientSocket.Connect(_ClientEndPoint);
byte[] MsgByte = new byte[1024];
MsgByte = Encoding.Unicode.GetBytes(txtmsg.Text);
_ClientSocket.Send(MsgByte);
Variables:
public const int _port = 8041;
public const string ttl = "255";
public IPAddress MulticastIP = IPAddress.Parse("239.0.0.222");
Thanks.
The router between you and the other LAN probably refuses to forward packets with multicast destination IPs. To handle multicast properly, the router itself has to be multicast-aware and implement protocols such as PIM (for coordinating multicast between routers) and IGMP (for coordinating multicast with end-hosts)
Your router probably doesn't forward multicast packages. In order for multicast to work all routers along the path of communication must me multicast enabled. Ping only requires the router to forward ping packages so that will really only tell you if you can reach the other computer at all. Take a look at this article to learn more about multicasting in C#.

Does UDP socket need to go through the Accept Process like TCP sockets?

I'm working with UDP and i was wondering about the Accept Method when multiple machine need to connect to a server. So far i was working with UDPCliente class, IPEndPoint class and BeginRecieve / EndRecieve Method to create a server where multiple machine can connect at the same time.
My question is simple do i need to use Accept Method to handler incoming connection and create a new socket for each new connection ?
What is the best way to handler multiple connection with UDP ?
The code samples i've seen so far create a new UDPClient class and a IPEndPoint where the server is listening for connections after that, the code call the BeginRecieve passing a function where the data is recieved and then starts the process of BeginRecieve again.
These are the code samples i've been using so far:
public static void receiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
byte[] receiveBytes = u.EndReceive(ar, ref e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
u.BeginReceive(new AsyncCallback(receiveCallback), s);
}
public static void receiveMessages()
{
IPEndPoint e = new IPEndPoint(IPAddress.Any, 5050);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
u.BeginReceive(new AsyncCallback(receiveCallback), s);
}
UDP is connectionless, so there's nothing to accept. If you need connections over UDP, then you have to implement them. Ideally, you would assign each "connection" some kind of identifier and include it in each datagram sent to the server. In some cases, it may be sufficient just to rely on the IP address and port to identify "connections".
But you can do it however you want. UDP treats each datagram as independent.
Short answer - no, you don't use accept() with UDP.
In UDP there are no connections, only datagrams. One side sends them and the other might receive them. Each datagram has information about the sender (IP address and port) that your server app can extract to differentiate the clients.

Sending data over UDP to a destination with multiple clients open

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.

DNS relay UDP on port 53

I noticed that BT Home are sending back fake DNS results from their DNS servers and this allows sites to bypass the IP addresses i have blocked in the firewall so i was looking to create my own DNS relay/server.
So far i can receive request on UDP port 53 and send them off to the DNS server and get a valid byte[] stream result and i then send back to the browser using the remote client port the request was made on but the browser just sends the request back again.
I've tested the code from a socket and the results work OK but for some reason IE/FF simply will not except the results.
public void Listen()
{
receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
receiveEndPoint = new IPEndPoint(IPAddress.Any, receivePort); receiveSocket.Bind(receiveEndPoint);
receivePort = (receiveSocket.LocalEndPoint as IPEndPoint).Port;
receiveBuffer = new byte[BufferSize];
receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
}
public void NetworkMessageReceivedCallback(IAsyncResult asyncResult)
{
EndPoint remoteEndPoint = null;
byte[] bytes = null;
remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); //Will contain the clients port
int bytesRead = receiveSocket.EndReceiveFrom(asyncResult, ref remoteEndPoint);
bytes = new Byte[bytesRead];
Buffer.BlockCopy(receiveBuffer, 0, bytes, 0, bytesRead);
//string ip = "208.67.222.222";
string ip = "192.168.1.254";
IPAddress dnsServer = IPAddress.Parse(ip);
Response R = Resolver.Lookup(bytes, dnsServer);
receiveSocket.SendTo(R.Message , remoteEndPoint);//127.0.0.1
receiveSocket.Close();
Listen();
}
I never dealt with raw DNS from C# but it looks like you are trying to resolve the bytes you received from the client, instead of just relaying them to the DNS server.
The message you read off the UDP socket contains a DNS query, not just a host name. Take a look at the RFC 2929 for what goes in there.
You might be interested in this little but great DNS filter - adsuck - by Marco Peereboom (though it's for Unix, not Windows).
Also, shouldn't your try and listen to UDP and TCP. I think UDP is used mostly for authoritative DNS queries.

Categories