1 / 10

Connectionless Sockets

SWE 344 Internet Protocols & Client Server Programming. Connectionless Sockets. Connectionless Sockets.

nanji
Download Presentation

Connectionless Sockets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 344 Internet Protocols & Client Server Programming Connectionless Sockets

  2. Connectionless Sockets 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.

  3. The UDP Server using System; using System.Net; using System.Net.Sockets; using System.Text; class SimpleUdpSrvr { public static void Main() { intrecv; byte[] data = new byte[1024]; IPEndPointipep = 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. The UDP Server 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); } } }

  5. The UDP Server 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 netstatcommand 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:\>

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

  8. A UDP Client { 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

More Related