Datagramsocket client-to-server Java instance

Source: Internet
Author: User

1 Importjava.io.IOException;2 ImportJava.net.DatagramPacket;3 ImportJava.net.DatagramSocket;4 Importjava.net.InetAddress;5 6  Public classtimeclient_udp{7       Public Static voidMain (String args[])throwsioexception{8String host = "localhost";9          byte[] msg =New byte[256];//storing and receiving dataTenInetAddress addr = inetaddress.getbyname (host);//Get host Address OneSystem.out.println ("Listening host is:" +addr); ADatagrampacket packet =NewDatagrampacket (msg,msg.length,addr,3000);//create a datagram to send and receive -Datagramsocket socket =NewDatagramsocket ();//Create a Socket object for communication -Socket.send (packet);//Send Datagrams thePacket =NewDatagrampacket (msg,msg.length);//Create a received datagram -Socket.receive (packet);//receive datagrams and store them in packet -String time =NewString (Packet.getdata ());//get the data content in a datagram -System.out.println (the current date for "Listen Host:" +host+ "is:" +Time ); +Socket.close ();//Release Socket -     } +}
Datagramsocket Client
1 Importjava.io.IOException;2 ImportJava.net.DatagramPacket;3 ImportJava.net.DatagramSocket;4 Importjava.net.InetAddress;5 Importjava.util.Date;6  Public classtimeserver_udp{7       Public Static voidMain (String args[])throwsioexception{8Datagramsocket socket =NewDatagramsocket (3000);//socket object. Port9System.out.println ("Waiting to receive data ...");Ten            while(true){ One                byteBuf[] =New byte[256]; ADatagrampacket packet =NewDatagrampacket (buf,buf.length); - socket.receive (packet); -                 theInetAddress addr =packet.getaddress (); -                intPort =Packet.getport (); -System.out.println ("received from" + addr.tostring () + ":" +port+ "Data"); -                 +String date =NewDate (). toString (); -BUF =date.getbytes (); +Packet =NewDatagrampacket (buf,buf.length,addr,port); A socket.send (packet); at           } -      } -}
Datagramsocket Service Side

Datagramsocket Client Java Project Project catalog is E:\EclipseProject\TimeClient_UDP.

Datagramsocket Server Java Project Project catalog is E:\EclipseProject\TimeServer_UDP.

Steps to run in DOS command-line cmd for Windows systems:

1. Configure the system environment variables

Java_home (add directory to store JDK) C:\Program files\java\jdk1.8.0_65

Path (Add bin subdirectory under JDK directory)%java_home%\bin

Classpath (Add the required packages in the Lib subdirectory of the JDK directory).; %java_home%\jre\lib;%java_home%\lib\dt.jar;%java_home%\lib\tools.jar;

2, open two DOS windows, respectively into the Datagramsocket client and service side of the project directory (the above directory)

3, the source code through the JAVAC project name, compile the. class file, then the Java project name, run

4, step 2,3 See figure 1DatagramSocket client running effect; Figure 2DatagramSocket Service side running effect.

Datagramsocket client-to-server Java instance

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.