-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient和Server双端通信
42 lines (40 loc) · 1.38 KB
/
Client和Server双端通信
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.net.*;
import java.io.*;
import java.util.Scanner;
class server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(4321);
System.out.println("Server OK");
while(true) {
Socket s = ss.accept();
InputStreamReader ins = new InputStreamReader(s.getInputStream());
BufferedReader in = new BufferedReader(ins);
String x = in.readLine();
System.out.println("Info from client: " + x);
PrintStream out = new PrintStream(s.getOutputStream());
out.println("hello client");
in.close();
out.close();
s.close();
}
} catch(IOException E) {}
}
}
class client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",4321);
PrintStream out = new PrintStream(s.getOutputStream());
String c = "ACM";
out.println(c);
InputStreamReader ins = new InputStreamReader(s.getInputStream());
BufferedReader in = new BufferedReader(ins);
String x = in.readLine();
System.out.println("Info from Server: " + x);
out.close();
in.close();
s.close();
} catch(IOException e) {}
}
}