作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的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/
我是一名优秀的程序员,十分优秀!