Java 中无法访问 UDP 客户端

发布于 2024-07-19 17:21:19 字数 375 浏览 7 评论 0原文

我正在运行一个简单的 UDP Java 服务器,它在连接时收集客户端的 IP 和端口,并将信息存储在数据库中。

客户端仍在监听服务器。 服务器停止。

之后,服务器想要重用数据库信息,到达客户端; 由于客户端仍在侦听服务器的同一端口,我想客户端应该接收通信。

我是UDP新手,请让我知道实现上述目标的方法。 谢谢。

让我重新表述一下这个问题,因为我确实尝试了 Stackoverflow 成员建议的方法。

服务器在很短的时间内可以联系到客户端,但是10分钟后客户端就无法访问了; 尽管客户端似乎已准备好一直监听服务器,但即使多次尝试,服务器也无法到达客户端。 这可能是什么原因造成的? 请让我知道处理这个问题的方法

I am running a simple UDP Java Server, which collects IP and Port of Client when connected, store information in Database.

Client is still listening to server. Server stops.

Later, server want to reuse the database information, to reach the client; and as the client is still listening to same port of server, I guess client should receive the communication.

I am new to UDP, please let me know the way to achieve the above objective. Thank you.

Let me rephrase the question as I did try the ways suggested by members of Stackoverflow.

The client can be contacted by server within a short timespan, but after say 10 mins the client is unreachable; although it appears client is ready to listen to server for all the time but the server cannot reach client even if tried for several time. What could be cause for this? please let me know the way to deal with this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2024-07-26 17:21:19

我认为您对 UDP 协议(​​RFC 768)有点困惑。 我认为回顾 UDP 协议以了解 UDP 和 TCP 之间的差异会很有帮助。

关于您的具体问题,如果没有任何类型的代码,很难知道您的确切问题是什么。 有一个 UDP 中的客户端-服务器示例 可用在阳光下的教程。

I think you are a bit confused regarding the UDP protocol (RFC 768). I think it would be helpful to review the UDP protocol to understand the differences between UDP and TCP.

Regarding your specific problem, it is difficult to know what is your exact problem without any type of code. There is a Client-Server in UDP example available in the sun tutorials.

固执像三岁 2024-07-26 17:21:19

UDP 是无会话的,所以我想它确实应该有效。

事情会是这样的:

// Client:

socket = new DatagramSocket();
DatagramPacket req = new DatagramPacket(data, data.length, serverAddress, serverPort);
socket.send(req);
DatagramPacket resp = new DatagramPacket(new byte[MAX_RESP_SIZE], MAX_RESP_SIZE);
socket.receive(resp);

// Server:

DatagramSocket socket = new DatagramSocket(port);
while (!stopped) {
    DatagramPacket req = new DatagramPacket(new byte[MAX_REQ_SIZE], MAX_REQ_SIZE);
    socket.receive(req);
    saveToDatabase(req.getAddress(), req.getPort());
}
socket.close();

// Then later:

DatagramSocket socket = new DatagramSocket(port);

// retrieve clientAddr and clientPort from database
DatagramPacket resp = new DatagramPacket(data, data.length, clientAddress, clientPort);
socket.send(resp);
socket.close();

UDP is sessionless, so I guess it should indeed work.

It would go something like that:

// Client:

socket = new DatagramSocket();
DatagramPacket req = new DatagramPacket(data, data.length, serverAddress, serverPort);
socket.send(req);
DatagramPacket resp = new DatagramPacket(new byte[MAX_RESP_SIZE], MAX_RESP_SIZE);
socket.receive(resp);

// Server:

DatagramSocket socket = new DatagramSocket(port);
while (!stopped) {
    DatagramPacket req = new DatagramPacket(new byte[MAX_REQ_SIZE], MAX_REQ_SIZE);
    socket.receive(req);
    saveToDatabase(req.getAddress(), req.getPort());
}
socket.close();

// Then later:

DatagramSocket socket = new DatagramSocket(port);

// retrieve clientAddr and clientPort from database
DatagramPacket resp = new DatagramPacket(data, data.length, clientAddress, clientPort);
socket.send(resp);
socket.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文