写一个java服务器和终端之间通信协议程序
时间: 2024-03-08 10:46:54 浏览: 24
好的,以下是一个简单的Java服务器和终端之间通信协议程序的代码示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
// 创建ServerSocket对象并监听端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
// 接受客户端连接请求
Socket socket = serverSocket.accept();
System.out.println("客户端已连接");
// 获取输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 接收客户端消息并向客户端发送响应
while (true) {
String message = in.readLine();
System.out.println("客户端发送的消息:" + message);
out.println("服务器响应:" + message);
if ("exit".equals(message)) {
break;
}
}
// 关闭流和连接
in.close();
out.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码实现了一个简单的服务器程序,监听端口为8888,接收客户端连接请求,获取输入输出流,接收客户端消息并向客户端发送响应,直到客户端发送exit指令断开连接。
以下是一个简单的Java客户端程序的代码示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
// 创建Socket对象并连接服务器
Socket socket = new Socket("127.0.0.1", 8888);
System.out.println("已连接服务器");
// 获取输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 向服务器发送消息并接收响应
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String message = input.readLine();
out.println(message);
String response = in.readLine();
System.out.println("服务器响应:" + response);
if ("exit".equals(message)) {
break;
}
}
// 关闭流和连接
input.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码实现了一个简单的客户端程序,连接服务器IP为127.0.0.1,端口为8888,获取输入输出流,向服务器发送消息并接收响应,直到发送exit指令断开连接。
使用以上代码,可以实现一个简单的Java服务器和终端之间通信协议程序。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)