java socket实现客户端服务端通信

作者ChihMinh,原作链接https://chihminh.github.io/2016/07/28/java-socket-commu/,转载请注明出处

java sockets源码一瞥中从代码的角度分析了Socket和ServerSocket,现在来实战一下

socket 实现客户端与服务端通信

客户端程序:

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
public class SocketClient {
public static void main(String[] args) {
try {
// 客户端与指定主机名和端口的服务端绑定
Socket socket = new Socket("127.0.0.1", 3600);
System.out.println("连接成功");
// 由socket对象得到输出流
PrintWriter printer = new PrintWriter(socket.getOutputStream());
// 由socket对象得到输入流,并将输入字节流转换为字符流
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 读取输入流的一行数据
String res = "";
String in = "";
while (!res.contains("bye") && !in.contains("bye")) {
// 读取系统输入,将其从字节流准换位字符流
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
in = sysin.readLine();
printer.println(in);
// 刷新输出流,使服务端马上收到字符串
printer.flush();
res = reader.readLine();
System.out.println("Server says: " + res);
}
System.out.println("连接关闭");
printer.close();
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

服务端程序:

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
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(3600);
while (true) {
Socket client = server.accept();
System.out.println("连接成功");
PrintWriter writer = new PrintWriter(client.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String res = "";
String in = "";
while (!res.contains("bye") && !in.contains("bye")) {
res = reader.readLine();
System.out.println("Client says: " + res);
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
in = sysIn.readLine();
writer.println(in);
writer.flush();
}
System.out.println("连接关闭");
reader.close();
writer.close();
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

这样同一时刻只能有一个客户端和服务端通信,下面用并发实现多个客户端同时与服务端通信。

多个客户端与服务端通信

客户端代码:

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
public class SocketClient {
public static void main(String[] args) {
try {
//
Socket socket = new Socket("127.0.0.1", 3600);
System.out.println("与服务端连接成功!");
// 得到输出流
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// 得到输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String output = "";
String input = "";
// 通信
while (!output.contains("bye") && !input.contains("bye")) {
System.out.print("Me: ");
// 从控制台输入
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
output = sysIn.readLine();
writer.println(output);
writer.flush();
input = reader.readLine();
System.out.println("Server: " + input);
}
// 关闭socket对象
writer.close();
reader.close();
socket.close();
System.out.println("与服务端连接断开!");
} catch (IOException e) {}
}
}

服务端代码:

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
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
private void execute(Socket socket) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
String input = "";
String output = "";
while (!input.contains("bye") && !output.contains("bye")) {
input = reader.readLine();
System.out.println("Client: " + input);
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
output = sysIn.readLine();
writer.println(output);
writer.flush();
}
reader.close();
writer.close();
socket.close();
System.out.println("与客户端连接断开!");
}
@Override
public void run() {
try {
execute(socket);
} catch (IOException e) {}
}
}

用线程池实现并发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ServerPool {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(3600);
ExecutorService pool = Executors.newFixedThreadPool(2);
while(true) {
Socket socket = server.accept();
System.out.println("与客户端连接成功!");
ServerThread serverThread = new ServerThread(socket);
pool.submit(serverThread);
}
} catch (IOException e) {}
}
}