UDP (user Datagram Protocol, Subscriber Datagram Protocol) is a commonly used transport layer protocol in the Internet, which provides the simplest protocol mechanism for sending messages to another user program. As with TCP, its default downlevel protocol is IP.UDP-oriented, does not provide commit and copy protection, and therefore does not guarantee the reliable transmission of data. UDP is generally used in high-reliability LANs.
. NET can use the socket class to write UDP-based network programs, Whenever you create a socket, select the third argument of the constructor function as the enumeration value PROTOCOLTYPE.UDP. In addition, the. Net System.Net.Sockets namespace also has a UdpClient class that can be specifically used for UDP network programming. -----Excerpt from < C#.NET framework Advanced Programming Technology Case Tutorial Guo Wenji et p163>
First, using the socket class
1 New int . Parse (Txtport.text)); 2 Socket New socket (addressfamily.internetwork, Sockettype.dgram, protocoltype.udp); 3 Serversocket.bind (IP);
Second, use the UdpClient class
1 IPAddress address = Ipaddress.parse (txtlocalip.text); 2 int port = int32.parse (txtlocalport.text); 3 New IPEndPoint (address, port); 4 New UdpClient (EndPoint);
-------------------------------------------------------------------here is the split line-----------------------------------------------
This paper mainly uses the first one, based on the socket to write a UDP packet program. The server continues to contract, and the client draws points on the form based on the received point data, as shown in the following:
One. Server-side
1. Create a socket object that binds the server-side IP and port number.
2. Create a thread for the user to obtain the IP and port number of the client. Basically, the client connects to the server and sends a "hello" message, and the server side can get the IP and port of the client.
1 Private voidbtnStart_Click (Objectsender, EventArgs e)2 {3 if(ServerSocket! =NULL)4 {5 return;6 }7IPEndPoint IP =NewIPEndPoint (Ipaddress.parse (Txtip.text),int. Parse (Txtport.text));8ServerSocket =Newsockets (AddressFamily.InterNetwork, Sockettype.dgram, protocoltype.udp);9 serversocket.bind (IP);Ten OneAppendtexttotxtlog (string. Format ("Server side is {0}, waiting for client connection", IP. ToString ())); AThread thread =NewThread (receivedata); -Thread. IsBackground =true; - thread. Start (); the -}
View Code
3. Send data
1 Private voidSendData ()2 {3 byte[] data =New byte[1024x768*1024x768];4 intj = +;5Random r =NewRandom ( -);6 while(J >0)7 {8j--;9 intx = R.next ( -);Ten inty = R.next ( -); One stringmsg = x.tostring () +","+y.tostring (); Adata =Encoding.Default.GetBytes (msg); - for(intK =0; K < Remotelist.count; k++) - { the inti =serversocket.sendto (data, data. Length, Socketflags.none,remotelist[k]); -Appendtexttotxtlog (string. Format ("the message sent to client {0} is {1}", Remotelist[k]. ToString (), msg)); - } - } +}
View Code
Client
1. Connect to the server side
1 Private voidBtnconnected_click (Objectsender, EventArgs e)2 {3 byte[] data =New byte[1024x768];4 5IPEndPoint IP =NewIPEndPoint (Ipaddress.parse ("192.168.10.179"),50000);6ServerSocket =Newsockets (AddressFamily.InterNetwork, Sockettype.dgram, protocoltype.udp);7 stringWelcome ="Hi there!";8data =Encoding.Default.GetBytes (welcome);9 serversocket.sendto (data, data. Length, Socketflags.none, IP);TenThread thread =NewThread (receivedata); OneThread. IsBackground =true; A thread. Start (); -}
View Code
2. Acceptance of data
1 /// <summary>2 ///Accept Message3 /// </summary>4 Private voidReceivedata ()5 {6 byte[] data =New byte[1024x768];7IPEndPoint EndPoint =NewIPEndPoint (Ipaddress.any,0);8EndPoint remote =(EndPoint) EndPoint;9 while(true)Ten { One intLen =0; A Try - { -Len = serversocket.receivefrom (data,refremote); the stringstr = Encoding.Default.GetString (data,0, Len); - string[] points = str. Split (','); - intx =int. Parse (points[0]); - inty =int. Parse (points[1]); +Point P =NewPoint (x, y); -Thread thread =NewThread (NewParameterizedthreadstart (Drawpoint)); +Thread. IsBackground =true; A thread. Start (p); at } - Catch(Exception ex) - { - - - } in } - to}
View Code
3. Draw
1 /// <summary>2 ///Drawing3 /// </summary>4 /// <param name= "p" ></param>5 Public voidDrawpoint (Objectp)6 {7 8Point point =(point) p;9Graphics g = This. Panel1. CreateGraphics ();TenPen pen =NewPen (color.black); OneG.drawellipse (pen, point. X, point. Y5,5); A}
View Code
SOURCE Download: http://download.csdn.net/detail/gongchao1212/9591239
Socket-based UDP packet sending program