Presentation is loading. Please wait.

Presentation is loading. Please wait.

UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you.

Similar presentations


Presentation on theme: "UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you."— Presentation transcript:

1 UDP Client-Server

2 The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you to perform both synchronous and asynchronous data transfer using any of the communication protocols listed in the ProtocolType enumeration. The Socket class follows the.NET Framework naming pattern for asynchronous methods. For example, the synchronous Receive method corresponds to the asynchronous BeginReceive and EndReceive methods.

3 One of the simplest types of machine-to-machine communication is the client-to- server with a static IP address. In the typical application, the server accepts a connection from any client, but the client must know the IP address of the server. The UdpClient class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways: 1.Create an instance of the UdpClient class using the remote host name and port number as parameters. 2.Create an instance of the UdpClient class and then call the Connect method. The UdpClient Class

4 The Connect method establishes a default remote host using the values specified in the port and hostname parameters. Once established, you do not have to specify a remote host in each call to the Send method. Establishing a default remote host is optional. Specifying a default remote host limits you to that host only. If you want to send datagrams to a different remote host, you must make another call to the Connect method or create another UdpClient without a default remote host. UdpClient.Connect Method

5 The IPEndPoint class contains the host and local or remote port information needed by an application to connect to a service on a host. By combining the host's IP address and port number of a service, the IPEndPoint class forms a connection point to a service. IPAddress.Any provides an IP address that indicates that the server must listen for client activity on all network interfaces. This field is read-only. The Any field is equivalent to 0.0.0.0 in dotted-quad notation. IPEndPoint

6 Communicating with a Byte Array

7 ProtocolType enumeration GgpGateway To Gateway Protocol IcmpInternet Control Message Protocol IdpInternet Datagram Protocol IgmpInternet Group Management Protocol IPInternet Protocol IPv6Internet Protocol version 6 (IPv6) IpxInternet Packet Exchange Protocol NDNet Disk Protocol (unofficial) PupPARC Universal Packet Protocol RawRaw IP packet protocol SpxSequenced Packet Exchange protocol SpxIISequenced Packet Exchange version 2 protocol TcpTransmission Control Protocol UdpUser Datagram Protocol UnknownUnknown protocol UnspecifiedUnspecified protocol Protocol types supported by the.NET Framework

8 using System; using System.Text; using System.Net; using System.Net.Sockets; namespace UDPClient { class Class1 { static void Main(string[] args) { string sendStr = ""; UdpClient theClient = new UdpClient("IP here", 9050); while (!sendStr.Trim().ToUpper().Equals("END")) { Console.Write("# "); sendStr = Console.ReadLine(); byte[] myData = new byte[1024]; myData = Encoding.ASCII.GetBytes(sendStr); theClient.Send(myData, myData.Length); } theClient.Close(); } UDP Client

9 using System; using System.Text; using System.Net; using System.Net.Sockets; namespace UDPServer { class Class1 { static void Main(string[] args) { string rcvData = ""; IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050); UdpClient theSock = new UdpClient(IPEP); IPEndPoint fromClient; while (!rcvData.Trim().ToUpper().Equals("END")) { byte[] myData = new byte[1024]; fromClient = new IPEndPoint(IPAddress.Any, 0); myData = theSock.Receive(ref fromClient); rcvData = Encoding.ASCII.GetString(myData); Console.WriteLine(fromClient.ToString() + " " + rcvData); } theSock.Close(); } UDP Server (Listener)

10 Peer-to-Peer Communications using System; using System.Threading; using System.Net; using System.Net.Sockets; using System.Text; namespace ClientServer_1 { class Chat1 { private static Thread clientThread; private static Thread serverThread; static void Main(string[] args) { CreateThreads(); } private static void CreateThreads() { clientThread = new Thread(new ThreadStart(RunClientThread)); clientThread.Start(); serverThread = new Thread(new ThreadStart(RunServerThread)); serverThread.Start(); }

11 private static void RunClientThread() { string sendStr=""; UdpClient theClient= new UdpClient("127.0.0.1",9050); while (!sendStr.Trim().ToUpper().Equals("END")) { sendStr=Console.ReadLine(); byte[] myData= new byte[10]; myData=Encoding.ASCII.GetBytes(sendStr); theClient.Send(myData,myData.Length); } theClient.Close(); } Client Thread

12 private static void RunServerThread() { string rcvData=""; IPEndPoint IPEP= new IPEndPoint(IPAddress.Any,9051); UdpClient theSock= new UdpClient(IPEP); IPEndPoint fromClient; while (!rcvData.Trim().ToUpper().Equals("END")) { byte[] myData= new byte[10]; fromClient= new IPEndPoint(IPAddress.Any,0); myData= theSock.Receive(ref fromClient); rcvData=Encoding.ASCII.GetString(myData); Console.WriteLine(fromClient.ToString() + " " + rcvData); } theSock.Close(); } Server Thread

13 Summary User Datagram Protocol (UDP) - permits a simple method of communication between two computers UDP does not check for transmission errors Client must know the IP address of the intended Server Server accepts connection from any Client The.NET Socket provides a set of methods for network communication Multi-Threaded Programming can be used for Peer-to-Peer Communication


Download ppt "UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you."

Similar presentations


Ads by Google