gpt4 book ai didi

java - 为什么我的简单 UDPServer 仅在第一次运行时才接收来自 UDPClient 的数据包?

转载 作者:行者123 更新时间:2023-12-02 07:30:54 25 4
gpt4 key购买 nike

我的UDP客户端将不断发送“Hello Server!”每 2 秒发送一次到 UDP 服务器(仅回显消息,但以大写形式)。他们都会打印收到的内容。

我的问题是我总是必须先运行服务器然后再运行客户端。如果我先运行客户端,然后运行服务器,服务器将根本不会收到任何内容。我希望服务器可以随时启动,并且仍然接收来自客户端的数据包。我怎样才能做到这一点?

UDP客户端:

import java.io.*;
import java.net.*;

class UDPClient
{
public static void main(String args[]) throws Exception
{
while(true) {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[100];
byte[] receiveData = new byte[100];
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();
Thread.sleep(2000);
}

}
}

UDP服务器:

import java.io.*;
import java.net.*;

class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[100];
byte[] sendData = new byte[100];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
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);
}
}
}

最佳答案

如果您先运行客户端,它将收到异常 PortUnreachableException 并且客户端程序将关闭,为了防止这种情况发生,您必须捕获该异常,以便客户端继续运行。

尝试:

import java.io.*;
import java.net.*;

class UDPClient
{
public static void main(String args[]) throws Exception
{
while(true) {
try{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[100];
byte[] receiveData = new byte[100];
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();
}
catch (PortUnreachableException pe)
{
System.out.println("COULDN'T CONNECT TO SERVER:" + pe.getMessage());
}
catch (Exception ex)
{
System.out.println("COULDN'T CONNECT TO SERVER:" + ex.getMessage());
}
Thread.sleep(2000);
}

}
}

关于java - 为什么我的简单 UDPServer 仅在第一次运行时才接收来自 UDPClient 的数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com