- public static void main(String[] args) throws Exception {
-
- while(true)
- {
- System.out.println("选择要进行的操作:1.输入字符串 2.退出");
- Scanner sc=new Scanner(System.in);
- int flag=sc.nextInt();
- switch (flag) {
- case 1:
- //选择操作字符串是进入connec函数
- connec();
- break;
- case 2:
- //退出时关闭虚拟机
- System.exit(0);
- break;
- default:
- System.out.println("输入非法选项");
- break;
- }
-
- }
- }
- public static void connec() throws Exception{
- Scanner sc=new Scanner(System.in);
- System.out.println("请输入要翻转的字符串");
- String s=sc.nextLine();
- //创建客户端的Socket
- Socket socket = new Socket("127.0.0.1", 12345);
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintStream ps = new PrintStream(socket.getOutputStream());
- //把键盘录入的字符串写到服务器去
- ps.println(s);
- //将服务器反转后的结果读取
- System.out.println(br.readLine());
- socket.close();
- }
- }
复制代码
|