Java socket Programming-UDP-based

Source: Internet
Author: User

Package Com.wzy.udptest;import Java.net.datagrampacket;import java.net.datagramsocket;import java.net.InetAddress;/** * * * @author Wzy * Server-side, implementation of UDP-based user login*/ Public classUdpserver { Public Static voidMain (string[] args) {Try{        //1. Create a server-side datagramsocket, specify the portDatagramsocket socket =NewDatagramsocket (8800); //2. Create a datagram to receive data sent by the client//creates a byte array that specifies the size of the received packet        byte[] data =New byte[1024x768]; Datagrampacket Packet=Newdatagrampacket (data, data.length); System. out. println ("server startup ..... "); //3. Receiving data sent by the client//This method will block until the datagram is receivedsocket.receive (packet); //4. Reading dataString info =NewString (data,0, data.length); System. out. println ("server-side received the message:"+Info.trim ()); /** * Response data to client **/        //1. Define the client's address, port number, dataInetAddress address =packet.getaddress (); intPort =Packet.getport (); byte[] Data2 ="Hello, client". GetBytes (); //2. Create a datagram that contains the data information for the responseDatagrampacket Packet2 =NewDatagrampacket (data2, Data2.length,address,port); //3. Responding to clientssocket.send (Packet2); //4. Close ResourcesSocket.close (); }        Catch(Exception E) {e.printstacktrace (); }                                    }}
Package Com.wzy.udptest;import Java.io.ioexception;import java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;import java.net.UnknownHostException;/** * Client*/ Public classUdpClient { Public Static voidMain (string[] args) throws IOException {/** * Send data to the server **/        //1. Define the server's address, port number, dataInetAddress address = Inetaddress.getbyname ("localhost"); intPort =8800; byte[] data ="user name: admin; Password: 123". GetBytes (); //2. Create a datagram that contains the sent data informationDatagrampacket packet =Newdatagrampacket (data, data.length, address, Port); //3. Create a Datagramsocket objectDatagramsocket socket =NewDatagramsocket (); //4. Sending datagrams to the serversocket.send (packet); /** * Receive server-side response **/        //1. Create datagrams to receive data from server-side responses        byte[] Data2 =New byte[1024x768]; Datagrampacket Packet2=NewDatagrampacket (data2, data2.length); //2. Accept data from server responsesocket.receive (Packet2); //3. Reading dataString reply =NewString (Data2,0, Packet2.getlength ()); System. out. println ("The client receives a response:"+reply); //4. Close ResourcesSocket.close (); }}

Java socket Programming-UDP-based

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.