UDP transport protocol in Java

Source: Internet
Author: User

 Package Pack;Import Java.io.BufferedReader;Import Java.io.IOException;Import Java.io.InputStreamReader;Import Java.Net.Datagrampacket;Import Java.Net.Datagramsocket;Import Java.Net.inetaddress;/* Public class Demo{     Public Static voidMain (string[] args) throws Exception {//TODO auto-generated method stub        //method1 ();} Public Static voidMethod1 () throws Exception {inetaddress i = inetaddress.getlocalhost ();        System.out.println (i.ToString ()); InetAddress ia = Inetaddress.getbyname ("Www.baidu.com");    System.out.println (Ia.gethostaddress ()); }} send receive data class udpsend {     Public Static voidMain (string[] args) throws Exception {//1. Creating a UDP service through Datagramsocket objectsDatagramsocket ds =NewDatagramsocket ();//2. Determining data, encapsulating into packagesbyte[] buf ="UDP send". GetBytes (); Datagrampacket DP =NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.2"),10000);//3. Send the existing data packets through the socket serviceDs.send (DP);    Ds.close (); }} class udpreceive {     Public Static voidMain (string[] args) throws Exception {//1. Creating a UDP socket, establishing an endpoint, specifying a portDatagramsocket ds =NewDatagramsocket (10000); while(true) {//2. Defining a packet for storing databyte[] buf =Newbyte[1024x768]; Datagrampacket DP =NewDatagrampacket (buf,buf.length);//3. Receiving data from a service's receiveDs.receive (DP);//4. Obtaining IP addresses and data from packet objectsString IP = dp.getaddress (). gethostaddress (); String data =NewString (Dp.getdata (),0, Dp.getlength ());            int port = Dp.getport (); System.out.println (ip+":"+data+":"+port);        Ds.close (); }}} keyboard input to send data class UdpSend2 {     Public Static voidMain (string[] args) throws Exception {Datagramsocket ds =NewDatagramsocket (); BufferedReader BUFR =NewBufferedReader (NewInputStreamReader (System.inch)); String line = Bufr.readline (); while(line!=NULL) {if("Over". Equals (line)) { Break;            } byte[] buf = Line.getbytes (); Datagrampacket DP =NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.10"),10001);        Ds.send (DP); }    }} class UdpRece2 {     Public Static voidMain (string[] args) throws Exception {Datagramsocket ds =NewDatagramsocket (100001); while(true) {byte[] buf =Newbyte[1024x768]; Datagrampacket DP =NewDatagrampacket (buf,buf.length);            Ds.receive (DP);            String IP = dp.getaddress (). gethostaddress (); String data =NewString (Dp.getdata (),0, Dp.getlength ()); System.out.println (ip+":"+data); }    }}*// * Chat program * / Public  class Demo {     Public Static voidMain (string[] args) throws Exception {//TODO auto-generated method stubDatagramsocket Sendsocket =NewDatagramsocket (); Datagramsocket Recesocket =NewDatagramsocket (10001);NewThread (NewSend (Sendsocket)). Start ();NewThread (NewRece (Recesocket)). Start (); }} class Send implements Runnable {    PrivateDatagramsocket ds; Send (Datagramsocket ds) { This. ds = ds; } Public voidRun () {Try{BufferedReader BUFR =NewBufferedReader (NewInputStreamReader (System.inch)); String line = Bufr.readline (); while(line!=NULL) {if("Over". Equals (line)) Break;            byte[] buf = Line.getbytes (); Datagrampacket DP =NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.10"),10001);            Ds.send (DP); }        }Catch(Exception e) {System.out.println ("Error"); }    }} class rece implements Runnable {    PrivateDatagramsocket ds; Rece (Datagramsocket ds) { This. ds = ds; } Public voidRun () {Try{ while(true) {byte[] buf =Newbyte[1024x768]; Datagrampacket DP =NewDatagrampacket (buf,buf.length);            Ds.receive (DP);            String IP = dp.getaddress (). gethostaddress (); String data =NewString (Dp.getdata (),0, Dp.getlength ()); System.out.println (ip+":"+data); }        }Catch(Exception e) {System.out.println ("Error"); }    }}

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

UDP transport protocol in Java

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.