Presentation is loading. Please wait.

Presentation is loading. Please wait.

Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming.

Similar presentations


Presentation on theme: "Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming."— Presentation transcript:

1 Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming

2 Because there is no connection between remote hosts, the UDP application cannot use the standard Send() and Receive() Socket methods. Instead, two new methods must be used, SendTo() and ReceiveFrom(). SendTo() The SendTo() method specifies the data to send and the IPEndpoint of the destination machine. ReceiveFrom() The ReceiveFrom() method has the same formats as the SendTo() method, with one important difference: the way the EndPoint object is declared. Connectionless Sockets

3 The UDP Server using System; using System.Net; using System.Net.Sockets; using System.Text; class SimpleUdpSrvr { public static void Main() { int recv; byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipep); Console.WriteLine("Waiting for a client..."); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); recv = newsock.ReceiveFrom(data, ref Remote);

4 Console.WriteLine("Message received from {0}:", Remote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); newsock.SendTo(data, data.Length, SocketFlags.None, Remote); while(true) { data = new byte[1024]; recv = newsock.ReceiveFrom(data, ref Remote); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); newsock.SendTo(data, recv, SocketFlags.None, Remote); } The UDP Server

5 When creating your sample UDP server, be careful that you don’t choose a UDP port that is already in use by another application on your machine. You can monitor the arrangement of applications that are listening on particular UDP ports by using the netstat command at a command prompt. C:\>netstat -a Active Connections Proto Local Address Foreign Address State TCP abednego:epmap 0.0.0.0:0 LISTENING TCP abednego:microsoft-ds 0.0.0.0:0 LISTENING TCP abednego:1025 0.0.0.0:0 LISTENING TCP abednego:1034 0.0.0.0:0 LISTENING TCP abednego:5000 0.0.0.0:0 LISTENING UDP abednego:1027 *:* UDP abednego:1035 *:* UDP abednego:9050 *:* UDP abednego:38037 *:* UDP abednego:38293 *:* UDP abednego:ntp *:* UDP abednego:1036 *:* UDP abednego:1900 *:* UDP abednego:12564 *:* C:\> The UDP Server

6 A UDP Client The UDP client program is similar to its partner server program. Because the client does not need to wait on a specific UDP port for incoming data, it does not use the Bind() method. Instead, it employs a random UDP port assigned by the system when the data is sent, and it uses the same port to receive return messages. If you are in a production environment, you might want to specify a set UDP port for the client as well, so that both the server and client programs use the same port numbers. using System; using System.Net; using System.Net.Sockets; using System.Text; class SimpleUdpClient { public static void Main() { byte[] data = new byte[1024]; string input, stringData;

7 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 Remote = (EndPoint)sender; data = new byte[1024]; int recv = server.ReceiveFrom(data, ref Remote); Console.WriteLine("Message received from {0}:", Remote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); while(true) A UDP Client

8 { input = Console.ReadLine(); if (input == "exit") break; server.SendTo(Encoding.ASCII.GetBytes(input), Remote); data = new byte[1024]; recv = server.ReceiveFrom(data, ref Remote); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); } Console.WriteLine("Stopping client"); server.Close(); }

9 Testing the Client and Server Programs Start the SimpleUdpSrvr program on the assigned server machine you will use for your testing. When the server program starts, it displays a short message: C:\>SimpleUdpSrvr Waiting for a client... C:\>SimpleUdpClient Message received from 127.0.0.1:9050: Welcome to my test server C:\>SimpleUdpSrvr Waiting for a client... Message received from 127.0.0.1:1340: Hello, are you there?

10 END


Download ppt "Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming."

Similar presentations


Ads by Google