The connection between the client and the server--the first lesson of java network programming

C/S program of TCP protocol

You need to use two classes to write the CS program of the TCP protocol. 1. ServerSocket to build the
server
2. Socket to build the client
Two parties use sockets (sockets, communication endpoints) to communicate

ServerSocket

Used to create a server. After creation, a port number will be bound.
Then the server can wait for the client to connect, and every time a client is connected, the server will get a new Socket object, which is used to communicate with the client.

Common construction methods:

ServerSocket(int port);Create a server based on the TCP/IP protocol and bind the specified port number.
Note: The range of the parameter port is: 0-65535 (1025-65535 is recommended)

Common methods:

Socket accept();Wait for the client to connect. This method will cause the thread to block until a new client connects successfully. After the Socket object is returned, the thread continues to execute.
void close();Release the occupied port number and shut down the server.

Socket

It is the endpoint of communication between two computers. It is a set of standards and a mechanism for the network driver to provide an interface for application programming.

Construction method:

Socket(String ip,int port)
creates a socket and connects to the server with the specified ip and port number.
Parameter 1. The ip address of the server.
Parameter 2. The port number of the server software...

Common methods:

- OutputStream getOutputStream();What is returned is that the output stream
- InputStream getInputStream();that points to the other end of the communication returns is that the input stream that points to the other end of the communication
- void close();closes the socket.
Note:
In network programming, the operation of obtaining input and output streams is for the client and server.
The input stream of the client is relative , and the input is the content of the output stream of the server. The output stream of the
client is output to the input stream of the server.
So when using it, you need to pay attention to the following rules: the client and the server get the stream The order must be reversed:
for example: the client gets the input stream first, then the server must get the output stream first

package com.test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //搭建服务器
        ServerSocket server = new ServerSocket(55565);
        System.out.println("服务器建立完毕,等待连接!");
        //等待客户端连接
        Socket socket =  server.accept();
        OutputStream os = socket.getOutputStream();
        PrintStream ps = new PrintStream(os);
        ps.println("欢迎连接服务器!");

        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String text = br.readLine();
        System.out.println("收到了客户端的消息:"+text);


        System.out.println("服务器连接完毕!");

    }
}
服务器建立完毕,等待连接!
收到了客户端的消息:你好,服务器
服务器连接完毕!
package com.test;

import java.io.*;
import java.net.Socket;

public class ClientDemo {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Socket socket = new Socket("127.0.0.1",55565);
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String text = br.readLine();
        System.out.println("收到消息:"+text);

        OutputStream os = socket.getOutputStream();
        PrintStream ps = new PrintStream(os);
        ps.println("你好,服务器");
    }
}
收到消息:欢迎连接服务器!

Guess you like

Origin blog.csdn.net/qq_38960155/article/details/108889306