[Java] 纯文本查看 复制代码
Socket socket = null;
PrintWriter pw = null;
Scanner sc = new Scanner(System.in);
InputStreamReader isr = null;
BufferedReader br = null;
public Client() {
try {
System.out.println("客户端");
socket = new Socket("127.0.0.1", 55555);
pw = new PrintWriter(socket.getOutputStream());
pw.println("aaaa");
//注意看这里
//pw.flush();
socket = new ServerSocket(12345).accept();
isr = new InputStreamReader(socket.getInputStream());
br = new BufferedReader(isr);
String word = br.readLine();
System.out.println(word);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(pw != null) {
pw.close();
}
try {
if( socket!= null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
[Java] 纯文本查看 复制代码
ServerSocket serverSocket = null;
Socket socket = null;
InputStreamReader isr = null;
BufferedReader br = null;
PrintWriter pw = null;
public Server() {
try {
System.out.println("服务器");
serverSocket = new ServerSocket(55555);
socket = serverSocket.accept();
isr = new InputStreamReader(socket.getInputStream());
br = new BufferedReader(isr);
String word = br.readLine();
System.out.println(word);
Socket socket = new Socket("127.0.0.1", 12345);
pw = new PrintWriter(socket.getOutputStream());
pw.println("连接成功");
//注意看这里
//pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(pw != null) {
pw.close();
}
try {
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}