Simple implementation of Java UDP communication

Source: Internet
Author: User

1, Java Implementation mode 1) server-side

/** * UDP Server side * */public class Udpserver {//define some constants private final intmax_length = 1024;//maximum receive byte length private fina   L intport_num = 5066;    Port number//byte array for storing received data private byte[] Recemsgs = new Byte[max_length];    Datagram sockets private Datagramsocket datagramsocket;       To receive the datagram private datagrampacket datagrampacket; Public udpserver () {try {/******* receives the data flow **///Creates a datagram socket and binds it to the specified port Datagramsoc            Ket = new Datagramsocket (port_num); Datagrampacket (byte buf[], int length), establishes a byte array to receive UDP packets Datagrampacket = new Datagrampacket (RECEMSGS, recemsg            S.LENGTH);                       Receive () to wait to receive UDP datagram Datagramsocket.receive (datagrampacket);            /****** Parse Datagram ****/string recestr = new String (Datagrampacket.getdata (), 0, Datagrampacket.getlength ());            System.out.println ("Server rece:" + recestr); System.out.println ("Server Port:" + datagrampacket.getport ());                       /***** return ACK Message datagram *//Assembly datagram byte[] buf = "I receive the message". GetBytes (); Datagrampacket sendpacket = new Datagrampacket (buf, Buf.length, Datagrampacket.getaddress (), datagrampacket.ge            Tport ());        Send Message Datagramsocket.send (sendpacket);        } catch (SocketException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();            } finally {//closes the socket if (datagramsocket! = null) {datagramsocket.close (); }        }    }}

2) Client

/*** * UDP client terminal ***/public class UdpClient {private String sendstr = "sendstring";    Private String netaddress = "127.0.0.1";       Private final intport_num = 5066;    Private Datagramsocket Datagramsocket;       Private Datagrampacket Datagrampacket;            Public udpclient () {try {/*** send data ***///Initialize Datagramsocket, note the difference from the previous server-side implementation            Datagramsocket = new Datagramsocket (); Send UDP datagrams using Datagrampacket (byte buf[], int length, inetaddress address, int port) function assembly byte[] buf = Sendstr.get            Bytes ();            InetAddress address = Inetaddress.getbyname (netaddress);            Datagrampacket = new Datagrampacket (buf, Buf.length, Address, port_num);                       Send data Datagramsocket.send (datagrampacket);            /*** receive Data ***/byte[] recebuf = new byte[1024];            Datagrampacket recepacket = new Datagrampacket (recebuf, recebuf.length); Datagramsocket.receive (recEpacket);            String recestr = new String (Recepacket.getdata (), 0, Recepacket.getlength ());            System.out.println ("Client rece Ack:" + recestr);                              System.out.println (Recepacket.getport ());        } catch (SocketException e) {e.printstacktrace ();        } catch (Unknownhostexception e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();            } finally {//closes the socket if (datagramsocket! = null) {datagramsocket.close (); }        }    }  }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple implementation of Java UDP communication

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.