The difference between invoking the UDP protocol and the TCP protocol in Java communication programming

Source: Internet
Author: User

The difference between the UDP protocol and the TCP protocol is no longer analyzed, mainly to analyze how these two protocols are used in Java communication programming.
First introduces TCP, which provides good support for the Tcp,java language. To establish TCP communication, you need to build the server first and
To the server's IP and port number.
Socket class under the TCP protocol:
The Java.net.Socket class represents the client connection
The Java.net.ServerSocket class represents a server-side connection
Server:serversocket ss = new ServerSocket (5000); Creating a server-side socket object
Socket socket = ss.accept (); Listening to connections
Client:socket socket = new Socket ("xxx.xxx.xxx.xxx", 5000); Creates the client's socket object, xxx.xxx.xxx.xxx represents the service
IP of the service device
Build the input and output:
Input: InputStream is = Socket.getinputstream (); Input stream, representing received information
Is.read (buffer);
Output: OutputStream OS = socket Socket.getoutputstream (); Output stream, which represents the information sent out
Os.write ("Welcome". GetBytes ()); Transfer in bytes
To close a resource:
Is.close ();
Os.close ();
Socket.close ();
Specific example: Using a TCP/IP socket (socket) for communication


The next step is to introduce UDP
UDP establishes a connection in a way that differs from TCP in that it only needs to establish a connection object and does not need to specify the server's IP and port number.
The socket class under the UDP protocol:
The Java.net.DatagramSocket class can either implement client connections or server-side connections.
The Java.net.DatagramPacket class implements encapsulation of data for transport. Under the UDP protocol, whether the data to be sent or the number to be received
are required to be processed into objects of type Datagrampacket.
Datagramsocket ds = new Datagramscoket (); If communication is required between the sending and receiving ends, the connection must be established
Listen port: Datagramsocket ds = new Datagramscoket (10010); InetAddress Senderip = revdp.getaddress (); int Senderport=rev
Dp.getport (); Get the IP and port number of the sending end
Building input and output
Output:
Byte[] B = "Hello". GetBytes ();
InetAddress Server = Inetaddress.getbyname ("xxx.xxx.xxx.xxx"); xxx.xxx.xxx.xxx is the IP of the receiving party
Datagrampacket SENDDP = new Datagrampacket (b,b.length,server,port); Port number for the receiver Port 10010
Ds.send (SENDDP);
Input:
byte[] data = new byte[1024];
Datagrampacket REVDP = new Datagrampacket (data, data.length);
Ds.receive (REVDP);
To close a resource:
Ds.close ()

Reference:java use of TCP and UDP traffic

The difference between invoking the UDP protocol and the TCP protocol in Java communication programming

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.