0% found this document useful (0 votes)
59 views

Writing UDP Client Server Program

This document contains code for writing both a UDP client and server program using the UdpClient class in C#. The server program code creates a Socket that binds to port 4400, receives data from any remote endpoint into a byte array, writes the received data to the console, and sends the data back to the remote endpoint. The client program code creates a Socket, sends strings entered by the user to the server endpoint, receives the echoed data back from the server and writes it to the console. The client continues in a loop until the user enters "exit".
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Writing UDP Client Server Program

This document contains code for writing both a UDP client and server program using the UdpClient class in C#. The server program code creates a Socket that binds to port 4400, receives data from any remote endpoint into a byte array, writes the received data to the console, and sends the data back to the remote endpoint. The client program code creates a Socket, sends strings entered by the user to the server endpoint, receives the echoed data back from the server and writes it to the console. The client continues in a loop until the user enters "exit".
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Writing UDP Client

Server Program
Client Server Programming

Server

Client

Server Process
Client process
Writing UDP Server Program using UdpClient
Class
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class UdpSocketServer
{
public static void Main()
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 4400);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
server.Bind(localEP);
Console.WriteLine("Waiting for a client...");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
int recv;
byte[] data;
while (true)
{
data = new byte[1024];
recv = server.ReceiveFrom(data, ref remoteEP);
Console.Write("Received from {0}: ", remoteEP.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
server.SendTo(data, recv, SocketFlags.None, remoteEP);
}
}
}
Writing UDP Client Program using UdpClient
Class
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SimpleUdpClient
{
public static void Main()
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint remoteEP = new IPEndPoint(IPAddress.Parse("10.20.0.231"), 4400);
byte[] data;
string input;
int recv;
while (true)
{
Console.Write("Enter message for server or exit to stop: ");
input = Console.ReadLine();
if (input == "exit")
break;
client.SendTo(Encoding.ASCII.GetBytes(input), remoteEP);
data = new byte[1024];
recv = client.ReceiveFrom(data, ref remoteEP);
Console.Write("Echo from from {0}: ", remoteEP.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
Console.WriteLine("Stopping client");
client.Close();
}
}

You might also like