1、掌握UDP的客户端Socket编程 2、掌握UDP的服务端编程 实验内容: 1、设计一个简单的UDP通信软件 2、使用UDP协议发送短消息 3、支持一对一、一对全体发送
时间: 2023-07-10 14:27:54 浏览: 51
好的,下面是Java中UDP通信的客户端和服务端编程示例:
UDP客户端编程:
```java
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = "Hello, server!";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("From Server: " + modifiedSentence);
clientSocket.close();
}
}
```
UDP服务端编程:
```java
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
```
这里是支持一对一、一对全体发送的简单UDP通信软件示例:
UDP客户端:
```java
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
if (sentence.equals("bye")) {
break;
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("From Server: " + modifiedSentence);
}
clientSocket.close();
}
}
```
UDP服务端:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
Map<InetAddress, Integer> clients = new HashMap<>();
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
String sentence = new String(receivePacket.getData());
if (!clients.containsKey(clientAddress)) {
clients.put(clientAddress, clientPort);
}
if (sentence.equals("all")) {
for (InetAddress address : clients.keySet()) {
int port = clients.get(address);
String response = "From " + clientAddress.toString() + ": " + sentence;
sendData = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
serverSocket.send(sendPacket);
}
} else {
int port = clients.get(clientAddress);
String response = "From " + clientAddress.toString() + ": " + sentence;
sendData = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, port);
serverSocket.send(sendPacket);
}
if (sentence.equals("bye")) {
clients.remove(clientAddress);
break;
}
}
serverSocket.close();
}
}
```
以上就是Java中UDP通信的客户端和服务端编程示例,希望对您有所帮助。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)