and communicate through java Socket ServerSocket

First talk about ServerSocket and Socket.

1.ServerSocket

ServerSocket is used to listen for client connection Socket class, if there is no connection would have been in a wait state.
ServetSocket has three constructors:

(1)

ServerSocket(int port);

The specified port to create ServerSocket, IP addresses using the default local IP address.

(2)

ServetSocket(int port,int backlog);

In addition to the port, there is a backlog queue length parameter for changing, specifying when the server is busy, the number of clients can be maintained with a connection request, the default is 50.

(3)

ServetSocket(int port,int backlog,InetAddress ip);

The specified port, backlog, ip address to create ServerSocket.

(Brief mention here InetAddress, InetAddress no constructor, only two static methods to obtain InetAddress object, namely,

getByName(String hostName);
getByAddress(byte[] address);

)

2.Socket

Socket used to connect to the server, the program makes a network request or reply request through a network Socket.
Socket three constructors:

(1)

Socket(InetAddress address,int port);

The specified IP and the Socket designated port is configured, for the local address, local ip default, dynamic allocation for the local port.

(2)

Socket(String address,int port);

And (1), the use of String represents the remote ip.

(3)

Socket(InetAddress address,int port,InetAddress localAddr,int localPort);

Create a designated remote ip, remote port, local ip, Socket local port.

(4)

Socket(String address,int port,InetAddress localAddr,int localPort)

And (2) Similarly, a String representing remote ip, ip expressed by local InetAddress.

You can write to understand the ServerSocket and Socket after a simple communication.

3. server

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
    public static void main(String[] args) throws IOException
    {
        ServerSocket serverSocket = new ServerSocket(12345);//这个端口自己随意,建议1024以上未使用的端口.
        while(true)
        {
            Socket socket = serverSocket.accept();//一直等待来自客户端的请求.
            PrintStream printStream = new PrintStream(socket.getOutputStream());//创建输出流
            printStream.println("Server message.");
            printStream.close();
            socket.close();
        }
    }
}

Talk about ServetSocket the accept () method:

Socket accept();

No parameters and returns a Socket, If you receive a Socket client is returned, otherwise has been in a wait state, the thread is blocked.

4. Client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

public class Client
{
    public static void main(String[] args) throws IOException
    {
        Socket socket = new Socket("127.0.0.1", 12345);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("This message comes from server:"+bufferedReader.readLine());
        bufferedReader.close();
        socket.close();
    }
}
Socket socket = new Socket("127.0.0.1", 12345);

Means creating a local address, port 12345 of Socket, created after the .accept server () method will receive this Socket, and creates output stream corresponding information, then the client obtains input from the Socket stream read, read the information from the server.

5. Run results

(Due to the author's beautiful terminal do not know what bad .... default terminal can only make use of the VScode ....)
to run server-side code:
Here Insert Picture Description
server side because accept () method will block until the customer sends the request.
Here Insert Picture Description
the client side of a run on the received information from the server.

6.Socket set timeout

You can set timeout to Socket, Socket more than this time when not connected to the system considers the connection failed.

Socket socket = new Socket("127.0.0.1",12345);
socket.setToTimeout(10000);//单位:ms,在这里是10s

But it can not be created before is not connected Socket object, and does not provide a specified timeout Socket constructor, so a common practice is to create a connectionless Socket, and then calls the connect () method to connect.

Socket socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1",12345),12345);

connect () method actually has two overloaded methods, namely,

void connect(SocketAddress endpoint);
void connect(SocketAddress endpoint,int timeout);

For the first connect () official documents did not mention a timeout and the like, it is a direct connection.
For the second connect, timeout specified timeout time in ms, compared to 0 to wait indefinitely.

7. Finally

This is the author of CSDN address
This is the author of the micro-channel public number, there are more exciting waiting for you to find articles welcome attention.
Here Insert Picture Description

Reference links
1. socket

Guess you like

Origin www.cnblogs.com/Blueeeeeeee/p/11701523.html