Java Learning--network programming

Source: Internet
Author: User

Udp

Encapsulates data and sources and purposes into packets without establishing a connection

Limit the size of each datagram within 64k

Because there is no connection, it is an unreliable agreement.

No need to establish connection, fast speed

Tcp

Establish a connection to form a channel for transmitting data

Make large data transfers in a connection

Complete connection via three handshake, is a reliable protocol

The connection must be established and the efficiency will be slightly lower

IP access:

Importjava.net.InetAddress;Importjava.net.UnknownHostException; Public classIpdemo { Public Static voidMain (string[] args)throwsunknownhostexception {//gets the local host IP address object. inetaddress IP =Inetaddress.getlocalhost (); //Gets the IP address object for the other host. ip = inetaddress.getbyname ("192.168.1.110"); //IP = inetaddress.getbyname ("My_think"); //IP = inetaddress.getbyname ("www.baidu.com");System.out.println (Ip.gethostaddress ());    System.out.println (Ip.gethostname ()); }}
View Code

UDP protocol send side :

* Create the send side of the UDP transport.
Ideas
* 1, set up a UDP socket service.
* 2, the data to be sent is encapsulated in the packet.
* 3, send data packets via UDP socket service.
* 4, close the socket service.

 Public classUdpsenddemo { Public Static voidMain (string[] args)throwsIOException {System.out.println ("Send side start ..."); //1,udpsocket Services. Use the Datagramsocket object. Datagramsocket ds =NewDatagramsocket (8888); //2, the data to be sent is encapsulated in the packet. String str = "UDP Transport Demo: Buddy's Coming!" "; //use Datagrampacket to encapsulate the data into the package.         byte[] buf =str.getbytes (); Datagrampacket DP=NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.100"), 10000); //3, send the data packets through the socket service of UDP. Use the Send method. Ds.send (DP); //4, close the resource. Ds.close (); }}
View Code

UDP protocol receive side :

* The idea of setting up a UDP receiving end.
* 1, to set up a UDP socket service, because it is to receive data, you must specify a port number.
* 2, create a packet to store the received data. It is convenient to parse the data with the method of the Packet object.
* 3, use the Receive method of the socket service to store the received data in a packet.
* 4. The data in the packet is parsed by means of a packet.
* 5, close resources

 Public classUdprecedemo { Public Static voidMain (string[] args)throwsIOException {System.out.println ("Receive-Side start ..."); //1, set up the UDP socket service. Datagramsocket ds =NewDatagramsocket (10000); //2. Create a packet.         byte[] buf =New byte[1024]; Datagrampacket DP=NewDatagrampacket (buf,buf.length); //3, use the Receive method to store the data in a packet. Ds.receive (DP);//a blocking type. //4, through the method of the packet object, parse the data, for example, address, port, data content. String IP =dp.getaddress (). gethostaddress (); intPort =Dp.getport (); String text=NewString (Dp.getdata (), 0, Dp.getlength ()); SYSTEM.OUT.PRINTLN (IP+ ":" +port+ ":" +text); //5, close the resource. Ds.close (); }}
View Code

Not finished .....

Java Learning--network programming

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.