TCP
In order to facilitate the development of network applications, Berkeley University in the United States implements a kind of communication protocol Socket on Unix, which enables programmers to access TCP/IP conveniently;
Use Socket of TCP/IP to communicate:
- The server program binds a socket to a specific port, and waits and listens for the connection request from the client to the port through the socket;
- The client program issues a connection request based on the host name and port of the server program.
Take 11.jpg (below) sent by the client to the server as an example:
Client:
import java.io.FileInputStream; import java.io.OutputStream; import java.net.Socket; public class Client { public static void main(String[] args) { try { //Establish link: specify the host address and corresponding port of the server (binding process) Socket socket = new Socket("127.0.0.1", 9999); //Using IO technology to send pictures OutputStream outputStream = socket.getOutputStream(); FileInputStream fileInputStream = new FileInputStream("D:\\11.jpg"); byte [] car = new byte[1024]; int length = 0; while((length = fileInputStream.read(car))!=-1) { outputStream.write(car, 0, length); } outputStream.flush(); outputStream.close(); fileInputStream.close(); //End of transfer, close link socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
Server side:
import java.io.FileOutputStream; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { //Establish server interface (binding process) ServerSocket serverSocket = new ServerSocket(9999); //Waiting to receive information from the client Socket socket = serverSocket.accept(); //Process the received pictures and generate them locally InputStream inputStream = socket.getInputStream(); byte [] car = new byte[1024]; int length = 0; FileOutputStream fileOutputStream = new FileOutputStream("D:\\22.jpg"); while((length=inputStream.read(car))!=-1) { fileOutputStream.write(car, 0, length); } fileOutputStream.flush(); fileOutputStream.close(); inputStream.close(); //End of operation, close link serverSocket.close(); } catch (Exception e) { e.printStackTrace(); } } }
We can see that TCP link transmission needs to establish a link between the client and the server first, and then transmit the data. Because of this, this method has a high accuracy of data transmission, and basically does not lose data. It is mainly used for accurate data transmission
UDP
-
UDP is a user datagram protocol. Socket programming based on UDP is a kind of connectionless socket communication, which provides connectionless and unreliable information transmission services.
-
Datagram socket: socket used to send and receive packets
-
Datagram packet: represents a packet
- Create a data Socket and specify a server port number.
- Provides a byte array for data storage.
- Call the receive() method of datagram packet to receive the data;
- Call the getData() method of datagram packet to get the data of byte array;
- Release resources.
Client:
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class Client { public static void main(String[] args) { try { //New socket binding port DatagramSocket socket = new DatagramSocket(9999); //Create a package packet to store the required data byte [] data = "Greetings from clients".getBytes(); InetSocketAddress address = new InetSocketAddress("127.0.0.1",8888); DatagramPacket packet = new DatagramPacket(data, data.length, address); //Send packet socket.send(packet); socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
Server side
import java.net.DatagramPacket; import java.net.DatagramSocket; public class Server { public static void main(String[] args) { try { //New socket binding port DatagramSocket socket = new DatagramSocket(8888); //Create the required packet to receive data byte [] car = new byte[1024]; DatagramPacket packet = new DatagramPacket(car, car.length); //receive data socket.receive(packet); int length = packet.getLength(); System.out.println(new String(car, 0, length)); //Close socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
Server console output:
We found that it is not necessary to establish a link before using UDP to transmit data, because the target host and port number will be stored in the packet, and the socket will only send the packet, and look for the client according to the information in the packet; therefore, UDP can transmit data one to many, and only need to send different hosts or port numbers to different packets Send in.
Note: UDP does not establish the link first, so UDP has a certain data loss rate (actually not high), but because the transmission speed is faster than TCP, it is often used to transmit some pictures, videos and other data that will not affect the use even if a little data is lost.
Summary: differences between TCP and UDP
- TCP is connection oriented and needs to establish a connection before sending data. UDP has no connection and does not need to establish a connection before sending data;
- The data transmitted by TCP connection is error free, not lost, not repeated, and arrives in order; UDP makes the best effort to deliver, and does not guarantee reliable delivery;
- UDP has better real-time performance and higher efficiency than TCP. It is suitable for high-speed transmission and high real-time communication or broadcast communication;
- Each TCP connection can only be point-to-point; UDP supports one-to-one, one to many, many to one and many to many interactive communication.