Java中TCP实现回显服务器及客户端的步骤如下:
1. 编写服务器端程序
服务器端需要完成以下任务:
- 创建ServerSocket对象
ServerSocket serverSocket = new ServerSocket(8888);
- 监听客户端的连接请求
Socket socket = serverSocket.accept();
- 读取客户端发送的数据
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
- 回显客户端发送的数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer, 0, len);
outputStream.flush();
完整代码如下:
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("EchoServer started.");
Socket socket = serverSocket.accept();
System.out.println("Accepted connection.");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
System.out.println("Received message: " + new String(buffer, 0, len));
OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer, 0, len);
outputStream.flush();
System.out.println("EchoServer stopped.");
socket.close();
serverSocket.close();
}
}
2. 编写客户端程序
客户端需要完成以下任务:
- 创建Socket对象,连接服务器
Socket socket = new Socket("localhost", 8888);
- 向服务器发送数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, EchoServer!".getBytes());
outputStream.flush();
- 读取服务器回显的数据
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
System.out.println("Received message: " + new String(buffer, 0, len));
完整代码如下:
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, EchoServer!".getBytes());
outputStream.flush();
System.out.println("Sent message: Hello, EchoServer!");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
System.out.println("Received message: " + new String(buffer, 0, len));
socket.close();
}
}
3. 示例说明
以下是一个简单的示例,演示了如何使用Java Socket API实现回显服务器:
// EchoServer.java
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("EchoServer started.");
Socket socket = serverSocket.accept();
System.out.println("Accepted connection.");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
System.out.println("Received message: " + new String(buffer, 0, len));
OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer, 0, len);
outputStream.flush();
System.out.println("EchoServer stopped.");
socket.close();
serverSocket.close();
}
}
// EchoClient.java
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Hello, EchoServer!".getBytes());
outputStream.flush();
System.out.println("Sent message: Hello, EchoServer!");
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
System.out.println("Received message: " + new String(buffer, 0, len));
socket.close();
}
}
运行服务器端程序后,控制台输出如下:
EchoServer started.
Accepted connection.
Received message: Hello, EchoServer!
EchoServer stopped.
运行客户端程序后,控制台输出如下:
Sent message: Hello, EchoServer!
Received message: Hello, EchoServer!
说明服务器端成功地回显了客户端发送的数据。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java中TCP实现回显服务器及客户端 - Python技术站