标签:boolean stat oid puts image inpu info class 字符
package com.banksteel.boot; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Test { public static void main(String[] args) { // socket ServerSocket serverSocket = null; PrintWriter out = null; Socket clientsSocket = null; Socket helloSocket = null; BufferedReader in = null; try { Boolean flag = true; while(flag) { // 1.端口号 serverSocket = new ServerSocket(9999); // 2.建立连接 clientsSocket = serverSocket.accept(); // 4.那边3建立连接后,开始输入内容 out = new PrintWriter(clientsSocket.getOutputStream(),true); System.out.println("a:请输入内容:"); // byte[] b = new byte[127]; // System.in.read(); // int n = System.in.read(b); // //转换为字符串 // String s = new String(b,0,n); Scanner sc=new Scanner(System.in); String s=sc.next(); // 得到内容 out.println(s); clientsSocket.close(); serverSocket.close(); helloSocket = new Socket("localhost", 9999); in = new BufferedReader(new InputStreamReader(helloSocket.getInputStream())); System.out.println("b:"+in.readLine()); in.close(); helloSocket.close(); flag = true; } } catch (Exception e) { // TODO: handle exception } } }
package com.guilf.springBoot_1; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Test { public static void main(String[] args) { // socket ServerSocket serverSocket = null; PrintWriter out = null; Socket clientsSocket = null; Socket helloSocket = null; BufferedReader in = null; try { Boolean flag = true; while(flag) { // 3.得到端口号,建立了连接 helloSocket = new Socket("localhost", 9999); // 得到内容 in = new BufferedReader(new InputStreamReader(helloSocket.getInputStream())); System.out.println("a:"+in.readLine()); in.close(); helloSocket.close(); // 设置端口号发送消息 serverSocket = new ServerSocket(9999); clientsSocket = serverSocket.accept(); out = new PrintWriter(clientsSocket.getOutputStream(),true); System.out.println("b:请输入内容:"); // byte[] b = new byte[127]; // System.in.read(); // int n = System.in.read(b); // //转换为字符串 // String s = new String(b,0,n); Scanner sc=new Scanner(System.in); String s=sc.next(); out.println(s); clientsSocket.close(); serverSocket.close(); flag = true; } } catch (Exception e) { // TODO: handle exception } } }
标签:boolean stat oid puts image inpu info class 字符
原文地址:https://www.cnblogs.com/guilf/p/9400502.html