Java study 1:the note of studying Socket which based UDP

Source: Internet
Author: User

    1. UDP Concept:

      UDP is the abbreviation of User Datagram protocol, the Chinese name is a Subscriber datagram protocol, is a connectionless Transport layer protocol in the OSI (Open System interconnection, open Systems Interconnect) Reference Model, Provides transaction-oriented, simple unreliable messaging services, and IETF RFC 768 is a formal specification for UDP. The protocol number for UDP in the IP message is 17. (from Baike)

    2. UDP Program:

      Because the UDP protocol is a no- Connect protocol,

      (1), each time the delivery of the figures do not need to kidnapped, only need to use the Datagrampacket () to build a good response to the packet on it;

      (2), there is no like TCP serversocket and socket, both sides are initialized with Datagrampacket;

(3), send and receive sent (packets), receive (packet)

Below the password:

*UDP does not have server and client points, but the code one sends an acceptance to the name (it doesn't matter ~ escape)

* After the success of the delivery, the two sides have established a link (virtual), can be mutually developed according to the

1 Importjava.io.IOException;2 ImportJava.net.DatagramPacket;3 ImportJava.net.DatagramSocket;4 Importjava.net.SocketException;5 6  Public classUdpserver {7      Public Static voidMain (string[] args) {8Datagramsocket DGs =NULL;9         Try {Tendgs=NewDatagramsocket (8888); One             //Receive A             byte[] buf=New byte[256]; -             intlength=256; -Datagrampacket dgp=Newdatagrampacket (buf, length); the dgs.receive (DGP); -System.out.println ("Receive from client:" +NewString (buf, 0, length)); -              -             //Send +String str= "Yeah I have receivered~"; -System.out.println (Dgp.getaddress ());//Use last time ' s packet to get the address + System.out.println (Dgp.getport ()); ADatagrampacket dgp_send=NewDatagrampacket (Str.getbytes (), Str.length (), dgp.getaddress (), Dgp.getport ()); at dgs.send (dgp_send); - dgs.close (); -}Catch(SocketException e) { - e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); in}finally{ - dgs.close (); to         } +     } -}
Udpserver
1 ImportJava.net.DatagramPacket;2 ImportJava.net.DatagramSocket;3 Importjava.net.InetAddress;4 5  Public classUdpClient {6      Public Static voidMain (string[] args) {7Datagramsocket DGs =NULL;8         Try {9dgs=NewDatagramsocket ();Ten             //Send->server OneString str= "So Dirty haha~"; ADatagrampacket dgp_send=NewDatagrampacket (Str.getbytes (), 0,str.length (), Inetaddress.getbyname ("localhost"), 8888); - dgs.send (dgp_send); -             //receive from server the             byte[] buf=New byte[256]; -Datagrampacket dgp2_receive=NewDatagrampacket (BUF, 256); - dgs.receive (dgp2_receive); -System.out.println ("Receive from server:" +NewString (buf,0,256)); + dgs.close (); -              +              A}Catch(Exception e) { atSystem.out.println ("Error:" +e); -         } -     } -}
UdpClient


The Send section of the P.s.i:server uses the packet DGP used to receive the packets to get the address and port of the transmitter, and then uses the new datagrampacket to send the numbers.

P.s.ii:datagrampacket conceptions, byte[] used to put the numbers, length used to put a long, simple program so the number of chaotic write (ha

  

Java study 1:the note of studying Socket which based UDP

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.