Networking
C# UDP Datagram Client and Server on the local computer
Jan 3rd
We will present a simple code for generating a UDP Datagram containing a simple word that will be sent from the local computer (UDP port 20000) and then read by a UDP server located on the same PC.
We’ll use C# as an example. The C#.NET language being based on a bytecode environment that achieves bounds checking on all arrays is the perfect networking programming language for beginners because they don’t also need to worry about stack buffer overflow vulnerabilities that may leave some unknown doors left open to exploiters. The only thing you need to worry about is networking.
THE SERVER: server.exe will take as arguments only the port number ( [PORT] ) which it will use for listening to UDP datagrams. The server also knows the port number of the client sending the datagram and of course its IP address which, of course, it is identical to its own IP address because all we’re trying to achieve here is to send and receive a UDP datagram from the same PC. So lets get through the process!
First we’ll use Dns.GetHostName() to obtain the host name of the local computer which will then be used to obtain the data structure that contains the local host’s internet address information. This data structure called IPHostEntry contains a list of IP addresses (IPHostEntry.AddressList) and Aliases (IPHostEntry.Aliases) that are associated with the host. After we obtain the IP address of the server by accessing the first element in the IP address list (IPHostEntry.AddressList[0]) we create a local IP/PORT pairing (IPEndPoint server_endpoint) that will inform the Socket through which we will listen at the client’s datagram about the server’s IP and listening PORT number, and a remote IP/PORT pairing (IPEndPoint remote_endpoint) which will capture the IP and PORT number of the client, whose datagrams we are listening to, after we’ll call the Socket.ReceiveFrom Method.
After we create the server_endpoint we need to associate it to the Socket object of the server in order to be able to read client’s UDP datagram. This association will be done using the Socket.Bind Method that works on both connectionless (ex:UDP) and connection-oriented protocols (ex: TCP, SCTP).
After this, the only thing that remains to be done is to simply read the UDP datagram by using the Socket.ReceiveFrom(Byte[] buffer, ref EndPoint ep) Method. ReceiveFrom reads (receives) the datagram and stores the source endpoint (client) in EndPoint ep.
SERVER.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
// SERVER.CS
// This is a simple UDP server that receives a UDP datagram containing a random word to a server on the local computer
// usage server.exe [PORT], where [PORT] is the sever UDP port number of the local PC
namespace UDPLocalServer
{
class ProgramServer
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("USAGE: client.exe [PORT] [MESSAGE]");
Environment.Exit(1);
}
byte[] message = new byte[128];
String server_name = Dns.GetHostName(); // Get the name of the sever
IPHostEntry server_host = Dns.GetHostEntry(server_name); // Internet host address information
IPAddress server_ip = server_host.AddressList[0]; // IP address of the server
IPEndPoint server_endpoint = new IPEndPoint(server_ip, Convert.ToInt16(args[0])); // IP and PORT pairing of the server
// Creates an IPEndPoint to capture the identity of the client when we'll use the Socket.ReceiveFrom Method
IPEndPoint remote_endpoint = new IPEndPoint(IPAddress.Any, 0); // IP and PORT pairing of the client
Socket server_udp_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Bind the Socket to the local endpoint
server_udp_socket.Bind(server_endpoint);
// Receive message from the remote local client
EndPoint ep = (EndPoint)remote_endpoint;
server_udp_socket.ReceiveFrom(message, ref ep);
Console.WriteLine(System.Text.Encoding.Unicode.GetString(message));
Console.WriteLine(ep.ToString());
}
}
}
THE CLIENT: client.exe will take as arguments the server’s port number ( [PORT] ) that will be used as the destination port for sending UDP datagrams and the word ( [MESSAGE] ) that will be sent to the server . The client will use local port number 20000 as the source port for sending the datagram and of course its IP address which, of course, it is identical to the UDP server’s IP address. Lets get through the client.cs code and analyze it.
As in the case of the UDP server, by using Dns.GetHostName() and IPHostEntry.AddressList[0] we’ll obtain the hostname and then the IP address of the local computer. Using this IP address and the port number 20000 we will create the local IP/PORT pairing (IPEndPoint client_endpoint) that will instruct Socket client_udp_socket to use this specific IP and PORT when we will send the datagram. Also we’ll need to create a remote IP/PORT pairing (IPEndPoint remote_endpoint) for the local Socket to know where to send the UDP datagram.
We then bind client_endpoint to Socket client_udp_socket. After this, we will use the Socket.Connect Method which takes remote_endpoint as argument in order to connect to the UDP server. Now we can send the message to the connected Socket (client_udp_socket) with the Socket.Send Method. Bare in mind though, we don’t necesarily need to use the Socket.Connect method to connect to the server in order to send UDP datagrams. Because UDP is a connectionless protocol we can skip this phase and send the datagram using the method Socket.SendTo which doesn’t need a connected socket in order to send the datagram.
CLIENT.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
// CLIENT.CS
// This is a simple client that sends a UDP datagram containing a random word on the local computer's UDP server
// usage client.exe [PORT] [MESSAGE], where [PORT] is the sever UDP port number of the local PC and [MESSAGE] is a random word
namespace UDPLocalClient
{
class ProgramClient
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("USAGE: client.exe [PORT] [MESSAGE]");
Environment.Exit(1);
}
Int16 port_number_client = 20000;
String client_name = Dns.GetHostName(); // Get the name of the client's PC
IPHostEntry client_host = Dns.GetHostEntry(client_name); // Internet host address information
IPAddress client_ip = client_host.AddressList[0]; // IP Address of client
IPEndPoint client_endpoint = new IPEndPoint(client_ip, port_number_client); // IP and PORT pairing the client
IPEndPoint remote_endpoint = new IPEndPoint(client_ip, Convert.ToInt16(args[0])); // IP and PORT pairing of the server
Socket client_udp_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Bind the Socket to the local endpoint
client_udp_socket.Bind(client_endpoint);
// Connect the UDP Socket to the server endpoint
client_udp_socket.Connect(remote_endpoint);
// Send the message
client_udp_socket.Send(System.Text.Encoding.Unicode.GetBytes(args[1]));
// Close the socket
client_udp_socket.Close();
Console.WriteLine(client_ip.ToString());
}
}
}







