BlueXIII's Blog

热爱技术,持续学习

0%

使用Java编写简单Socket客户端和服务端

简介

某同事要用Socket连接外围系统,帮忙写了个简单的Demo。
包含一个客户端和一个服务端的,仅是示例,比较简陋,服务端也没有用多线程。
直接贴代码了。

服务端

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.net.*;

/**
* Socket服务端示例
*/
public class SocketServer {
ServerSocket serverSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;

void runServer() {
try {
// 创建服务端监听
serverSocket = new ServerSocket(31313, 10);

// 等待客户端连接
System.out.println("等待客户端连接...");
connection = serverSocket.accept();
System.out.println("收到客户端连接: " + connection.getInetAddress().getHostName());

// 获取输入输出流
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());

// 连接成功后,首先向客户端发成功消息
sendMsg("连接成功");

// 发送接收消息
do {
try {
message = (String) in.readObject();
System.out.println("client>" + message);

// 发送退出消息
if (message.equals("bye")) {
sendMsg("bye");
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
System.err.println("数据格式无效");
}
} while (!message.equals("bye")); //当对方消息为bye时退出循环
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 关闭Socket连接
try {
in.close();
out.close();
serverSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}

void sendMsg(String msg) {
try {
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String args[]) {
SocketServer server = new SocketServer();
while (true) {
server.runServer();
}
}
}

客户端

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
import java.net.*;

/**
* Socket客户端示例
*/
public class SocketClient {
Socket clientSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;

void runClient() {
try {
// 连接到服务端
clientSocket = new Socket("localhost", 31313);
System.out.println("已连接到服务端");

// 获取输入输出流
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(clientSocket.getInputStream());

// 发送接收消息
do {
try {
// 接收消息
message = (String) in.readObject();
System.out.println("server>" + message);

// 发送消息
sendMsg("hello");
sendMsg("hello again");

// 发送退出消息
message = "bye";
sendMsg(message);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
System.err.println("数据格式无效");
}
} while (!message.equals("bye")); //当对方消息为bye时退出循环
} catch (UnknownHostException ex) {
ex.printStackTrace();
System.err.println("无法连接到服务端");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 关闭Socket连接
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

void sendMsg(String msg) {
try {
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String args[]) {
SocketClient client = new SocketClient();
client.runClient();
}
}