1. TCP communication
For connection oriented communication, the client and server must shake hands three times and establish a logical connection before communication (Security)
1.1 communication steps
The server starts first. The server will not actively request the client. The client must request the server. The client and the server establish a logical connection. This connection contains an IO object, and the client and the server can use the IO object for communication. The communication data is not just characters, so the IO object is a byte stream object
The server side must specify two things:
1. Multiple clients interact with the server at the same time. The server must specify which client to interact with. There is a method accept on the server to obtain the requested client object
2. When multiple clients interact with the server at the same time, they need to use multiple IO stream objects, but the server has no IO stream. The server can obtain the requested client object Socket and use the IO stream provided in each client Socket to interact with the client. In short, the server uses the client stream to interact with the client
1.2 client Socket class
java.net.Socket:
This class implements a client socket (also known as a "socket"). A socket is the endpoint of communication between two machines.
Socket: a network unit that contains an IP address and port number
Construction method:
Socket(String host, int port): create a stream socket and connect it to the specified port number on the specified host.
Parameters:
String host: server host name / server IP address
int port: the port number of the server
Member method:
OutputStream getOutputStream(): returns the output stream of this socket.
InputStream getInputStream(): returns the input stream of this socket.
void close(): close this socket.
Client implementation steps:
1. Create a client object Socket and bind the IP address and port number of the server in the construction method
2. Use the method getOUtputStream in the Socket object to obtain the network byte output stream OutputStream object
3. Use the method writer in the network byte output stream OutputStream object to send data to the server
4. Use the method getInputStream in the Socket object to obtain the network byte input stream InputStream object
5. Use the read method in the InputStream object of the network byte input stream to read the data written back by the server
6. Release resources
be careful:
1. When interacting with the server, the client must use the network flow provided in the Socket, not the self created flow object
2. When the client object Socket is created, it will request the server to establish a connection path through three handshakes. At this time, if the server is not started, an exception will be thrown. If it has been started, it can interact normally.
Code example:
public static void main(String[] args) throws IOException { // Create a client Socket object and bind the IP address and port number of the server in the construction method Socket socket = new Socket("localhost", 7788); // Use the method getOutputStream in the Socket to obtain the network byte output stream OutputStream object OutputStream os = socket.getOutputStream(); // Use the method write in the network byte stream OutputStream to send data to the server os.write("Send data to server".getBytes()); // Use the method getInputStream in the Socket to obtain the network byte input stream object InputStream InputStream is = socket.getInputStream(); // Use the read method in the network byte stream InputStream to read the data written back by the server byte[] b = new byte[1024]; int len = is.read(b); System.out.println(new String(b, 0, len)); // Release resources socket.close(); }
1.3 server socket
java.net.ServerSocket: this class implements the server socket
Function: receive the request from the client, read the data sent by the client, and write back the data to the client
Construction method:
ServerSocket(int port): creates a server socket bound to a specific port
The server needs to know which client requested the server. You can use the accept method to obtain the requested client object Socket
Member method:
Socket accept(): listen and accept connections to this socket.
Server implementation steps:
1. Create the server ServerSocket object and the port number to be specified by the system
2. Use the accept method in ServerSocket to obtain the requested client object Socket
3. Use the method getInputStream in the Socket object to obtain the network byte input stream object InputStream object
4. Use the read method in the network byte input stream InputStream to read the data sent by the client
5. Use the method getOutputStream in the Socket object to obtain the network byte output stream object OutputStream object
6. Use the write method in the network byte input stream OutputStream to write back data to the client
7. Release resources
Code example:
public static void main(String[] args) throws IOException { // Create the server ServerSocket object and the port number to be specified by the system ServerSocket server = new ServerSocket(7788); // Use the accept method in ServerSocket to obtain the requested client object Socket Socket socket = server.accept(); // Use the method getInputStream in the Socket object to obtain the network byte input stream object InputStream object InputStream is = socket.getInputStream(); // Use the read method in the network byte input stream InputStream to read the data sent by the client byte[] b = new byte[1024]; int len = is.read(b); System.out.println(new String(b, 0, len)); // Use the method getOutputStream in the Socket object to obtain the network byte output stream object OutputStream object OutputStream os = socket.getOutputStream(); // Use the write method in the network byte input stream OutputStream to write back data to the client os.write("The server writes back data to the client".getBytes()); // Release resources socket.close(); server.close(); }