1 / 13

UDP Client-Server

UDP Client-Server. The Socket Class.

ronnie
Download Presentation

UDP Client-Server

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. 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 synchronousReceive method corresponds to the asynchronousBeginReceive and EndReceive methods.

  3. The UdpClient Class • 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: • Create an instance of the UdpClient class using the remote host name and port number as parameters. • Create an instance of the UdpClient class and then call the Connect method.

  4. UdpClient.Connect Method 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.

  5. IPEndPoint 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.

  6. Communicating with a Byte Array

  7. ProtocolType enumeration Protocol types supported by the .NET Framework Ggp Gateway To Gateway Protocol Icmp Internet Control Message Protocol Idp Internet Datagram Protocol Igmp Internet Group Management Protocol IP Internet Protocol IPv6 Internet Protocol version 6 (IPv6) Ipx Internet Packet Exchange Protocol ND Net Disk Protocol (unofficial) Pup PARC Universal Packet Protocol Raw Raw IP packet protocol Spx Sequenced Packet Exchange protocol SpxII Sequenced Packet Exchange version 2 protocol Tcp Transmission Control Protocol Udp User Datagram Protocol Unknown Unknown protocol Unspecified Unspecified protocol

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

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

  10. Peer-to-Peer Communications using System; using System.Threading; using System.Net; using System.Net.Sockets; using System.Text; namespace ClientServer_1 { classChat1 { privatestaticThread clientThread; privatestaticThread serverThread; staticvoid Main(string[] args) { CreateThreads(); } privatestaticvoid CreateThreads() { clientThread = newThread(newThreadStart(RunClientThread)); clientThread.Start(); serverThread = newThread(newThreadStart(RunServerThread)); serverThread.Start(); }

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

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

  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 networkcommunication Multi-Threaded Programming can be used for Peer-to-Peer Communication

More Related