()编写java基于 TCP 协议的程序。编写一个服务器端程序,该程序可接收多个客户端谜的请求。编写一个客户端程序,该程序可向服务器发送一些文本消息。服务器端程序需要使用多线程。 (2)使用UDP 协议,编写程序实现(1)的功能
时间: 2023-06-26 22:05:55 浏览: 107
1. 基于 TCP 协议的程序
服务器端程序:
```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 TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket socket = serverSocket.accept();
new Thread(new ServerThread(socket)).start();
}
}
}
class ServerThread implements Runnable {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String line;
while ((line = in.readLine()) != null) {
System.out.println("客户端发送的消息:" + line);
out.println("服务器收到了你的消息:" + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
客户端程序:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要发送的消息:");
String line = scanner.nextLine();
out.println(line);
String response = in.readLine();
System.out.println("服务器返回的消息:" + response);
}
}
}
```
2. 基于 UDP 协议的程序
服务器端程序:
```java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
byte[] buffer = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
serverSocket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("客户端发送的消息:" + message);
DatagramPacket response = new DatagramPacket(("服务器收到了你的消息:" + message).getBytes(),
("服务器收到了你的消息:" + message).getBytes().length, packet.getAddress(), packet.getPort());
serverSocket.send(response);
}
}
}
```
客户端程序:
```java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要发送的消息:");
String line = scanner.nextLine();
byte[] data = line.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("localhost"), 8888);
socket.send(packet);
byte[] buffer = new byte[1024];
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
socket.receive(response);
String message = new String(response.getData(), 0, response.getLength());
System.out.println("服务器返回的消息:" + message);
}
}
}
```
阅读全文