socket simulation client and server

Posted by jsnyder2k on Thu, 22 Aug 2019 07:13:17 +0200

Idea: Client first receives data from keyboard, then converts it into output stream, then into print stream, sends it in security, receives data from server, reads it by line, and finally closes input and output, releases resources.
The server receives data by line, processes it, converts it into output stream and then into print stream, sends it safely, and finally closes the input and output, releases resources.

Scenario: Store between buffers, fast implementation, good user experience, search, registration account.

As shown in the figure:

socket is an abstraction layer through which applications can send or receive data and open, read, write and close files.
// Sockets allow applications to insert I/O into the network and communicate with other applications in the network. A network socket is a combination of IP addresses and ports.

public class Client{
	public static void main(String[] args) throws IOException{
		Socket socket = new Socket();
		socket.setSoTimeout(3000);   The timeout time is 3 seconds
		socket.connect(new InetSocketAddress(Inte4Address.getLocalHost(),port=2000),timeout=3000);
		System.out.println("Print local address and port number"+socket.getLocalAddress()+"p:"+socket.getLocalPort());
		try{
			accdata(socket);
		}
		catch(Exception e){
			System.out.println("Abnormal closure");
		}
		socket.close();
		System.out.println("Client has exited");
	}
	
	public static void accdata(Socket client) throws Exception{
		
//		The BufferedReader class reads text from the character input stream and buffers characters to read characters, arrays and rows effectively.
		InputStrem = System.in;
		BufferedReader input = new BufferedReader(new InputStream(in);
		
//		Get the socket output stream and convert it to print stream
		OutputStream outputStream = client.getOutputStream();
		PrintStream socketPrintStream = new PrintStream(outputStream);
		
//		The return data of the server
		InputSteam inputStream = client.getInputStream();
		BufferedReader socketBufferedReader = new BufferedReader(new InputStream(intputSteam);
		
		boolean flag = true;
		
		while(flag){
//			The keyboard reads a line and sends it to the server
//			If automatic refresh is enabled by printwriter, it is possible to do this only by calling one of the methods of println, printf, or format.
//			Not every time a newline character happens to be output. These methods use the platform-owned line separator concept, rather than newline characters. Print to the server.
			String str = input.readLine();
			socketPrintStream.println(str);
			
//			Data returned by the server is read by line
			String echo = socketBufferedReader.readLine();
			if("bye".equalsIgnoreCase(echo)){
				flag = false;
			}
			else{
				System.out.println(echo);
			}
			
			socketPrintStream.close();
			socketBufferedReader.close();
		}
	}
}

public class server{
	public static void main(String[] args) throws IOException{
		ServerSocket server = new ServerSocket(port:2000);
		
		
		while(true){
			Scocket client = server.accept();
			//Client builds asynchronous threads
			ClientHandler clientHandler = new ClientHandler(client);
			clientHandler.start();
		}
	}
	
	private static class ClientHandler extends Thread{
	`	private Socket socket;
		private boolean flag = true;
		ClientHandler(Socket socket){
			this.socket = socket;
		}
	}
	
	public void run(){
		super.run();
		System.out.println("d"+socket.getIntetAddress()+
		"p:"+socket.getPort());
		
		try{
//		Converting to Print Stream
		printStream socketoutput = new PrintStream(socket.getOutputStream());
		
		BufferedReader socketInput = new BufferedReader
		(new InputStreamReader(socket.getInputStream()));
		
		while(flag){
			String str = socketInput.readLine();
			if("bye".equalsIgnoreCase(str)){
				flag = false;
				socketOutput.println(x:"bye");
			}
			else{
				System.out.println(str);
				socketOutput.println("Send back"+str.length());
			}
		}
			socketInput.close();
			socketOutput.close();
		}
		
		catch(Exception e){
			System.out.println("Abnormal disconnection of connection");
		}
			try{
				socket.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			
			System.out.println("d"+socket.getIntetAddress()+
			"p:"+socket.getPort());
			
		}
		
	}
}

Asynchronism: A mode of communication for equipment requirements. The standard communication interface provided by our PC is asynchronous.
Asynchronous parties do not need a common clock, that is, the receiver does not know when the sender will send, so in the message sent, there must be a message prompting the receiver to start receiving, such as the start bit, and at the end there is a stop bit.

Tcp protocol:
TCP shakes hands three times as follows:
The client sends the SYN (SEQ=x) message to the server and enters the SYN_SEND state.
The server receives the SYN message, responds to a SYN (SEQ=y) ACK (ACK=x+1) message, and enters the SYN_RECV state.
The client receives the SYN message from the server, responds to an ACK (ACK=y+1) message and enters the Established state.
After three handshakes, the TCP client and server successfully established a connection and began to transmit data.

Topics: socket network