Socket socket-1

1. Socket

1. Concept:
1) Socket: Also called "socket", it is mainly used for network communication (network communication consists of two blocks: server and client)
to describe IP address and port number information.
        2) Server and
client The server usually uses ServerSocket to monitor the connection of the client.
At the same time, the client uses Socket to send a request to connect to the server.
2. The use of sockets
1) The ip address and port number to send the request should be specified in the client.
Port number: 1-1000, usually the port number within 1024 is reserved by the operating system and should not be used, and
a number greater than 1024 can be used;
2) The server needs to specify the port number to be monitored and wait for the connection at the same time. 3. Network communication can use Socket for information transmission. InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream();                 Read and write data through corresponding input and output.





2. Socket-Client

1. Client: Client Socket socket = new Socket("localhost",8888); 1) getLocalPort(): Get the local port number; 2) getPort(): Get the port number of the remote server; 2. Socket's Common API InetAddress add= socket.getLocalAddress();//Get the local address   add.getHostAddress()    add.getLocalHost()    add.getHostName() 








3. Socket-Server  

1. Server: ServerSocket 
ServerSocket server = 
new ServerSocket(8888);//Create a server object and specify the port number as 8888
2. Call the accept() method to monitor the client's connection, which has a blocking effect.
Namely: Socket socket = server.accept();


Fourth, communicate with each other

1.Client1:

public class Client {
	public static void main(String[] args) {
		try {
			Socket socket = new Socket("localhost",8888);//The client sends a request to connect,
		    /** Also specify the ip address and port number to send the request to.
		     * Common API of Socket socket
		     */
			// get the local address
			InetAddress add= socket.getLocalAddress();
			System.out.println("host address"+add.getHostAddress());
			System.out.println("localhost"+add.getLocalHost());
			System.out.println("主机名:"+add.getHostName());
			/*
			 * getLocalPort():
			 * Get the local port number
			 */
			System.out.println("Local port number:"+socket.getLocalPort());
			/*
			 * getPort():
			 * Get the port number of the remote server
			 */
			System.out.println("Port number:"+socket.getPort());
			
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}

2.Server1

         ServerSocket ss=new ServerSocket(8888);//Create a server object and specify the port number

public class Server {
	public static void main(String[] args) {
		try {
			//Create a server object and specify the port number as 8888
			ServerSocket server =
				new ServerSocket(8888);
			/*
			 * Call the accept() method to listen to the client's connection,
			 * method has blocking effect.
			 */
			System.out.println("---waiting for client connection------");
			Socket socket = server.accept();
			System.out.println("---A client connected successfully------");
			
			System.out.println("Port number:"+socket.getPort());
			System.out.println("Local port number:"+socket.getLocalPort());
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}
        3. Execution results

                    ①Run the server first ②Then run the client

                                

              





Guess you like

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