-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client和Server双端通信(线程版)
56 lines (53 loc) · 1.58 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
serv p = new serv(s);
Thread t = new Thread(p);
t.start();
}
} catch(IOException E) {}
}
}
class serv implements Runnable {
Socket s;
static int i;
public serv(Socket s1) {
s = s1;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String info = in.readLine();
++i;
System.out.println("info from client " + i);
PrintStream out = new PrintStream(s.getOutputStream());
out.println("You are number " + i);
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);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String x = in.readLine();
System.out.println("Info from Server: " + x);
out.close();
in.close();
s.close();
} catch(IOException e) {}
}
}