Socket network communication

1. Socket communication has a wide range of applications. Linux, Windows, and Java are all using Socket for network communication.

2. Two java programs can exchange data through a two-way network communication connection. One end of this two-way link is called a socket

3. The two classes Socket and ServerSocket defined in the java.net package are used to realize the client and the Service side of the two-way connection respectively.

4. The addressing information required to establish a connection is the IP address and port number of the remote computer [port number] (different applications on this IP address are distinguished by the port number)

5, TCP port and UDP port are separated

 

How to communicate?

First, use ServiceSocket to open a port on the server side ServiceSocket serviceSocket = new ServiceSocket(6666), so that the server side starts to listen to the 6666 port, any application that accesses this port from the client side will establish a link between the two, so, in the Client Socket s = new Socket("127.0.0.1",6666); (the function of this line: use a port of this machine (this port is randomly generated, anyway, the link has been established, do not care about the port of this machine) ) to access the port 6666 on the server side to establish a link between the two.) But the key to whether this connection can be established is still in the statement Socket s = ss.accept(); The client sends a request to establish a connection, only After calling this method, the connection will be established successfully.

127.0.0.1 is the IP address of all local machines.

Let's take a look at the client-server code of the simplest synchronization test;

Service-Terminal:

public class TCPServer{

public static void main(String[] args) throws Exception{

1,ServerSocket ss = new ServerSocket(6666);

while(true){

 3, Socket s = ss.accept(); //Use an infinite loop to accept the client's request

 5,System.out.println("a client connect!");

 5, DataInputStream dis = new DataInputStream(s.getInputStream());//Use the input stream to accept the io stream sent from the server

 5    System.out.println(dis.readUTF);

  5   dis.close();

 5    s.close();

}

}

}

Note: accept() is a blocking test. For example, if there are two programs on the client or users A and B access the server, A accesses first, but there is a statement such as Sleep (30000000) in A, and the accept method is called on the server. After that, it will be blocked for a long time, and B will not be able to access the server. It must wait for the loop in while() to complete before continuing to access the server.

How to solve this problem: use asynchronous test.

Client:

public class TCPClient {

public static void main(String[] args){

 2,Socket s = new Socket("127.0.0.1",6666);

 4,OutputStream os = s.getOutputStream();

  4 DataOutputStream dos = new DataOutputStream(os); //Use the output stream on the client to communicate with the server

 4 //sleep(30000000); //Sleep(300000000) as mentioned above, the above problem will occur!

 4    dos.writeUTF("hello server!");

 4    dos.flush();

 4    dos.close();

 4    s.close();

}

}

A server port can have multiple client connections, but multiple servers with the same port cannot be opened. For example, the code that continuously compiles and runs two servers will return an error!

The code execution order is like the numbers in the code. (To run the Server first, then run the Client).

Of course, the server can also pass data to the client to achieve two-way transmission of data

server:


client:


Since the server reads first and then writes, the client must write first and then read. Otherwise, since the readUTF method is blocking, it will always wait for the client's data, but the client is also reading, thus falling into an infinite wait.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325680395&siteId=291194637