The core of network programming UDP and TCP programming How are the two protocols TCP and UDP used in Java communication programming

How are the two protocols TCP and UDP used in Java communication programming

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 introduce TCP, for TCP, Java language provides good support for it. To establish TCP communication, you first need to build a server and
get the IP and port number of the server.
Socket class under TCP protocol:
java.net.Socket class represents client connection
java.net.ServerSocket class represents server connection
Server: ServerSocket ss = new ServerSocket(5000); //Create a server-side socket object
              Socket socket = ss. accept(); //Listen to the connection
Client: Socket socket = new Socket ("xxx.xxx.xxx.xxx", 5000); //Create a Socket object of the client, xxx.xxx.xxx.xxx represents the IP of the
              server
Build input and output:
Input: InputStream is = socket.getInputStream(); //input stream, representing the received information
            is.read(buffer);
output: OutputStream os = socket socket.getOutputStream(); //output stream, representing The information sent out
            os.write("Welcome".getBytes()); //
Close the resource with byte transmission:
is.close();
os.close();
socket.close();
Specific example: use TCP/IP socket (Socket) for communication 


Next, let's introduce the way UDP
UDP establishes a connection is different from the way of TCP, It only needs to establish a connection object, and does not need to specify the IP and port number of the server.
Socket class under the UDP protocol: The
java.net.DatagramSocket class can implement both client-side connection and server-side connection.
The java.net.DatagramPacket class implements data encapsulation for transmission. Under the UDP protocol, both the data to be sent and the
data need to be processed into objects of type DatagramPacket.
DatagramSocket ds = new DatagramScoket(); // Establish a connection, if the sender and the receiver need to communicate, they need to
listen : DatagramSocket ds = new DatagramScoket(10010); InetAddress senderIP = revDp.getAddress(); int senderPort=rev
Dp.getPort(); //Get the IP and port number of the sender to
construct the input and output
output:
byte[] b = "Hello".getBytes();
InetAddress server = InetAddress.getByName("xxx.xxx.xxx .xxx"); //xxx.xxx.xxx.xxx is the receiver's IP
DatagramPacket sendDp = new DatagramPacket(b,b.length,server,port); //port为接收方的端口号10010
ds.send(sendDp);
输入:
byte[] data = new byte[1024];
DatagramPacket revDp = new DatagramPacket(data, data.length);
ds.receive(revDp);
关闭资源:
ds.close()

UDP协议与TCP协议之间的区别不再分析,主要是分析一下这两个协议在Java通信编程中是如何被使用的。
首先介绍TCP,对于TCP,Java语言为它提供了良好的支持。建立TCP通信,首先需要构建服务器,并且得
到服务器的IP和端口号。
TCP协议下的Socket类:
java.net.Socket类代表客户端连接
java.net.ServerSocket类代表服务器端连接
Server:ServerSocket ss = new ServerSocket(5000); //创建服务器端的socket对象
              Socket socket = ss.accept(); //监听连接
Client:  Socket socket = new Socket ("xxx.xxx.xxx.xxx", 5000); //创建客户端的Socket对象,xxx.xxx.xxx.xxx代表服
              务器的IP
构建输入输出:
输入:InputStream is = socket.getInputStream(); //输入流,代表接收到的信息
            is.read(buffer);
输出:OutputStream os = socket socket.getOutputStream(); //输出流,代表发送出去的信息
            os.write("Welcome".getBytes()); //以字节传输
关闭资源:
is.close();
os.close();
socket.close();
具体实例:使用TCP/IP的套接字(Socket)进行通信 


接下来介绍一下UDP
UDP建立连接的方式与TCP的方式不同,它只需要建立一个连接对象即可,不需要指定服务器的IP和端口号。
UDP协议下的Socket类:
java.net.DatagramSocket类既可以实现客户端连接,也可以实现服务器端连接。
java.net.DatagramPacket类实现对于传输的数据封装。在UDP协议下,无论是需要发送的数据还是需要接收的数
据都需要被处理成DatagramPacket类型的对象。
DatagramSocket ds = new DatagramScoket(); //建立连接, 如果发送端和接收端需要通信,那么在建立连接时需要监
听端口:DatagramSocket ds = new DatagramScoket(10010); InetAddress senderIP = revDp.getAddress(); int senderPort=rev
Dp.getPort(); //获得发送端的IP和端口号
构建输入输出
输出:
byte[] b = "Hello".getBytes();
InetAddress server = InetAddress.getByName("xxx.xxx.xxx.xxx"); //xxx.xxx.xxx.xxx为接收方的IP
DatagramPacket sendDp = new DatagramPacket(b,b.length,server,port); //port为接收方的端口号10010
ds.send(sendDp);
输入:
byte[] data = new byte[1024];
DatagramPacket revDp = new DatagramPacket(data, data.length);
ds.receive(revDp);
关闭资源:
ds.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128815&siteId=291194637