Simple Java UDP connectivity test

Source: Internet
Author: User

UDP does not specifically provide a server-side API as TCP does, all with Datagramsocket and packet datagrams. So UDP is connectionless, because all the links are in the datagram, let Datagramsocket to send.

Server End:

Import Java.net.DatagramPacket; Import Java.net.DatagramSocket; /** * @author wangking e-mail:admin717@gmail.com * @version Date Created: 2009-9-16 03:25:29 * Class Description */public class Udpserver {/* * * @param args */public static void main (string[] args) throws Exception {byte[] buf = new byte[1024]; Datagramsocket socket = new Datagramsocket (5678); Datagrampacket packet = new Datagrampacket (buf,buf.length); while (true) {socket.receive (packet); System.out.println (New String (buf,0,buf.length)); } } }

Client-side tests:

Import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress;/** * @author Wangking e-mail:admin717@gmail.com * @version Date Created: 2009-9-16 03:30:11 * Class Description */public class UdpClient {public static VO ID Main (string[] args) throws Exception {datagramsocket socket = new Datagramsocket (9999); byte[] buf = "HHH". GetBytes (); Datagrampacket packet = new Datagrampacket (buf,0,buf.length,new inetsocketaddress ("127.0.0.1", 5678)); Socket.send (packet); Socket.close (); } }

Related Article

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.