1. Basic network concepts
First, clarify a concept: network programming! = Website programming, network programming is now generally called TCP/IP programming.
2. Network communication protocol and interface
3. Communication protocol layering idea
4. Reference model
V. IP protocol
Everyone's computer has a unique IP address so that the error message will not be transmitted when communicating with each other.
An IP address is divided into four segments by one dot. The IP address inside the computer is represented by four bytes. One byte represents a segment, and the maximum number represented by each byte can only reach 255.
VI. TCP protocol and UDP protocol
TCP and UDP are located on the same layer and are both based on the IP layer. Since the two computers have different IP addresses, the two computers can be distinguished and can talk to each other. There are generally two ways to talk: the first is TCP and the second is UDP. TCP is a reliable connection. TCP is like making a phone call. You need to call the other party first and wait for the other party to respond before continuing to talk to the other party. That is, you must confirm that you can send the message before sending the message. TCP uploads anything reliable. As long as a connection is established on two machines, the data sent on the machine will definitely be transmitted to the other party's machine. UDP is like sending a telegram, and it will be done if it is sent out. It doesn't matter whether the other party receives it or not, so UDP is unreliable. Although TCP transmits data reliably, it is transmitted slowly. UDP transmits data unreliable, but it is transmitted quickly.
7. Socket programming
Generally, network programming is called Socket programming, and Socket means "socket" in English.
Install a socket on both computers, and then plug the two ends of a cable into the sockets of the two computers, so that the two computers can establish a connection. This socket is the Socket.
Because they can communicate with each other, I said that you are my server, but in a logical sense, I should send the things to you first, and then you will handle and forward them. So you call it Server. But in a technical sense, only TCP will divide Server and Client. For UDP, in a strict sense, there are no so-called Server and Client. The socket of TCP Server is called ServerSocket, and the socket of Client is called Socket.
When two computers are connected to each other, you must first know their IP addresses, but providing only IP addresses is not enough. You must also have the port number of the connection, that is, which application to connect to.
The port number is used to distinguish different applications on a machine. The port number occupies 2 bytes inside the computer. There are up to 65536 port numbers on a machine. An application can occupy multiple port numbers. If the port number is occupied by an application, other applications will no longer be able to use this port number. Remember, if the program we write should occupy the port number, if it wants to occupy the port number above 1024, do not occupy the port number below 1024, because the system may be requisitioned at any time. The port number itself is divided into TCP port and UDP port. TCP port 8888 and UDP port 8888 are two completely different ports. There are 65536 TCP ports and UDP ports.
8. TCP Socket Communication Model
9. Socket usage examples
ServerSocket
import java.net.*;import java.io.*;public class TestServerSocket{ public static void main(String args[]) throws Exception{ ServerSocket ss = new ServerSocket(6666); /* When creating a ServerSocket object, it is often assigned a port number. The meaning is to use which port number the new ServerSocket object should use and which port number to listen to the client's connection. Therefore, the meaning of specifying a port number is to tell the computer where the ServerSocket object listens to the client's connection. */ * The server side receives the client's connection requests continuously, so the server-side programming is generally a dead loop and runs endlessly. */ while(true){ Socket s = s.accept(); /*Call the accept() method on the server side to accept the client's connection object. The accept() method is a blocking method. I have been waiting foolishly for whether a client applies for connection. Then the Socket socket on the server side establishes a connection with the Socket socket on the client. */ /* Whether the client can connect to the server side depends on whether the server side accepts the client's connection request. If the client accepts the connection request, then install a Socket socket on the server side to establish a connection with the client through this socket, and communicate with each other. */ System.out.println("A Client Connected!"); /* Use the InputStream stream to receive information sent from the client, and use the DataInputStream data stream to process the received information*/ DataInputStream dis = new DataInputStream(s.getInputStream()); /* Use readUTF(method reads all the received information and stores it in the variable str to read the readUTF() method is also a blocking method. It will foolishly wait for the client to send information, and then reads the received information. If the client does not write something, it will wait foolishly on the server side until the client writes something. The blocking method is often not efficient. For example, a client is connected, but it has not sent information for a long time. Then the server-side program is blocked, so that the other client cannot connect, because if another client wants to connect to the server, it must call the accept() method on the server side. However, the accept() method must be called in the next loop. Now the server-side program is blocked when it is called readUTF(). It has to wait for the client that has been connected to send the information and read it out. If the client does not send the information to the server side, then the readUTF() method will not be able to read the information. Then the server-side program will block here and cannot perform the next loop, so that other clients cannot connect to the server side*/ String str = dis.readUTF(); System.out.println(str); } } Client Socket
import java.net.*;import java.io.*;public class TestClientSocket{ public static void main(String args[]) throws Exception{ Socket s = new Socket("127.0.0.1",6666); /*Client applies to connect to the Server side*/ /*After connecting to the server side, you can output information to the server side and receive information returned from the server side, and receive information output information and receive return information. Both the stream input and output principle must be used to process the information*/ /* Here is the output stream OutputStream output information to the server side*/ OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); Thread.sleep(30000);/*The client sleeps for 30 seconds before sending information to the server*/ dos.writeUTF("Hello Server!"); }}The client requests a connection to the server through port 6666. After the server accepts the client's connection request, it installs a Socket on the server and then connects this Socket with the client's Socket, so that the server can communicate with the client. When another client applies for a connection, after the server accepts it, another Socket will be installed to connect to the client's Socket.