TCP: Transmission control protocol (TCP), is one of the main protocols of the Internet protocol group. It has its roots in the original network implementation, where it complemented the Internet protocol.
User Datagram Protocol (UDP) is a connectionless transport layer Protocol that provides a simple and unreliable transaction-oriented information transfer service, ensuring communication efficiency and low transmission delay.
Socket:Socket programming is used to solve the problem of communication between the client and the server.
TCP:
Server:
1) Create a ServerSocket instance and specify a local port to listen for connection requests.
2) Call the Accept () method of the socket to obtain the client connection request, and establish the connection with the client through the socket instance returned by the Accept () method.
3) Obtain InputStream and OutputStream through the returned Socket instance, and write and read data.
4) When data transmission ends, call the close() method of the socket instance to close the connection.
Client:
1) Create a Socket instance and establish a connection through the specified server address and port.
2) Obtain InputStream and OutputStream through the Socket instance for data reading and writing.
3) After data transmission, call the close() method of the socket instance to close the connection.
UDP:
Server:
1) Specify the local port to create DatagramSocket instance
2) Create DatagramPacket instance through byte array, call DatagramSocket instance receive() method, use DatagramPacket instance to receive data
3) Set the data returned by the DatagramPacket instance and call the send() method of the DatagramSocket instance to send the data
4) When data transfer is complete, call the close() method of the DatagramSocket instance
Client:
1) Create DatagramSocket instance
2) Create DatagramSocket instance with IP address port and data, call DatagramSocket instance send() method to send data packets
3) Create DatagramSocket instance from byte array, call DatagramSocket instance receive() method to receive packet
4) When data transfer is complete, call the close() method of the DatagramSocket instance
Socket programming API interface:
(1) Create socket: the socket() function
Function prototype:
int socket(int af, int type, int protocol);
Copy the code
-
Af: indicates the IP address type, AF_INET and AF_INET6 are commonly used
-
Type: Indicates the data transmission mode, which is SOCK_STREAM (TCP) and connectionless (SOCK_DGRAM) (UDP).
-
Protocol: indicates the protocol used by the socket. Common protocols are IPPROTO_TCP and IPPTOTO_UDP, which indicate the TCP and UDP transport protocols respectively.
(2) Bind sockets: bind()
Function prototype:
int bind(int sock, struct sockaddr *addr, socklen_t addrlen);
Copy the code
- Sock parameter: sock is the socket file descriptor.
- The addr argument: addr is a pointer to the sockADDR structure variable.
- Addrlen parameter: addrlen is the sizeof the addr variable, which can be calculated from sizeof().
(3) Establish a connection: connect() function
Function prototype:
int connect(int sock, struct sockaddr *serv_addr, socklen_t addrlen);
Copy the code
The arguments are similar to those of bind().
(4) Listen: the listen() function
Function prototype:
int listen(int sock, int backlog);
Copy the code
- Sock parameter: Sock indicates the socket that needs to enter the listening state.
- Backlog parameter: Backlog is the maximum length of the request queue.
(5) Receiving requests: Accept () function
Function prototype:
int accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
Copy the code
- Sock parameter: Sock is a server socket.
- Addr parameter: addr is the sockadDR_in structure variable.
- Addrlen parameter: Addrlen is the length of the parameter addr, which can be obtained from sizeof().
- Return value: A new socket used to communicate with the client.
(6) Close: the close() function
Function prototype:
int close(int fd);
Copy the code
- Fd: The file descriptor to close.
The HTTP protocol:
HTTP hypertext transfer protocol (HTTP) is a standard protocol used for communication between clients and servers. HTTP mainly specifies how the client establishes a connection to the server, how the client requests data from the server, how the server responds to the request, and finally how the connection is closed.
Establish a connection: The client uses DNS and load balancing technologies to find a server based on the URL entered by the user, and establishes a TCP connection with port 80 of the server
Make a request: The client sends a message to the server to request a page specified in the URL and to perform a specified operation, including GET, post, and head
Response: The server sends a response to the client. The response begins with a status code.
Close the connection: Either the client or the server can close the connection. Each request is made using a separate network connection
Disadvantages:
(1) Plaintext communication is not encrypted, so the content may be eavesdropped
(2) If the identity of the communication party is not verified, it may be disguised
(3) Packet integrity cannot be verified and may be tampered with
The HTTPS protocol:
Hypertext Transfer Security protocol. The HTTP channel aims at security. In short, it is the secure version of HTTP, that is, SSL layer is added under HTTP. The security foundation of HTTPS is SSL, so the details of encryption need SSL. HTTP and HTTPS are two different protocols.
Main functions:
1) Establish an information security channel to ensure the security of data transmission;
2) Verify the authenticity of the website.
The difference between HTTP and HTTPS
1. HTTPS protocol requires certificate application. Generally, there are few free certificates, so it requires a certain cost.
2. HTTP is a hypertext transfer protocol, and information is transmitted in plain text. HTTPS is a Secure Sockets Layer (SSL) encryption transfer protocol.
3. HTTP and HTTPS use completely different connections and use different ports, the former 80 and the latter 443.
4. HTTP connections are simple and stateless; HTTPS is a network protocol that uses SSL and HTTP to encrypt transmission and authenticate identity. It is more secure than HTTP.
Conclusion: All articles have been organized into a document, click me to get more Java basic knowledge manual, can also pay attention to the subsequent post.