Network programming client lowercase is converted to uppercase by the server TCP interactive mode

The client input letters are sent to the server, and the server (the server adopts multi-threaded or single-threaded) receives it and displays it on the console, and converts the data to uppercase and returns it to the client until the client inputs over and the conversion ends.

Client

/**
 * 客户端程序
 * 
 * @author Wxl
 *
 */
public class TCPClient {
    
    
	public static void main(String[] args) {
    
    
		try {
    
    
			Socket socket = new Socket("127.0.0.1", 2333);
			System.out.println("连接成功服务端成功");
			System.out.println("请输入一个小写字母,服务器将转化为大写");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
			BufferedReader br2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String str1 = null;
			while ((str1 = br.readLine())!=null) {
    
    
				if (str1.equals("over")) {
    
    
					break;
				}
				pw.println(str1);
				pw.flush();
				System.out.println("转换为大写-->"+br2.readLine());
			}
//			br2.close();
//			br.close();
			socket.close();
			System.out.println("连接结束!!!");
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
	}
}

Server

/**
 * 服务端程序
 * 
 * @author Wxl
 *
 */
public class TCPServer {
    
    
	public static void main(String[] args) {
    
    
		try {
    
    
			ServerSocket ss = new ServerSocket(2333);
			System.out.println("服务端开始监听... ");
			System.out.println("连接成功,服务器开始接受数据");
			Socket socket = ss.accept();
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
			String data = null;
			while ((data=br.readLine())!=null) {
    
    
				System.out.println("已接收到数据-->" + data);
				pw.println(data.toUpperCase());
			}
			socket.close();
			ss.close();
			System.out.println("本次连接结束!!!");
		} catch (IOException e) {
    
    
			e.printStackTrace();
			
		}
		
	}
}

Run screenshot

Insert picture description here
Insert picture description here

First open the server, then open the client!!!

muah

Guess you like

Origin blog.csdn.net/xiaole060901/article/details/108206974