Java UDP 服务器无法解码正确的 IP 地址
我正在尝试使用 java UDP 制作客户端服务器应用程序。当服务器从客户端接收消息时,它应该解码 IP 地址和端口号,以便可以发回数据。问题是当从数据包中解码 IP 地址时,前面有一个 /,因此它无法返回消息。程序的输出如下
等待数据 已收到:消息 /178.179.35.1 56798
我应该如何从数据包中删除正在解码的IP地址开头的/?谢谢!
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[1024];
byte[] sendData = new byte[1024];
while(true) {
System.out.println("waiting for data");
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();
System.out.println(IPAddress);
SocketAddress newtry = receivePacket.getSocketAddress();
int port = receivePacket.getPort();
System.out.println(port);
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, newtry);
serverSocket.send(sendPacket);
}
}
}
I am trying to make a client server application using java UDP. when the server takes in the message from the client it should decode the IP address and port number so that it can send back the data. the problem is when the IP address is decoded from the packet is has a / in the front so it cannot return a message. the output for the program is as follows
waiting for data
RECEIVED: message
/178.179.35.1
56798
how should I remove the / from the begining of the IP address being decoded from the packet? THANKS!
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[1024];
byte[] sendData = new byte[1024];
while(true) {
System.out.println("waiting for data");
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();
System.out.println(IPAddress);
SocketAddress newtry = receivePacket.getSocketAddress();
int port = receivePacket.getPort();
System.out.println(port);
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, newtry);
serverSocket.send(sendPacket);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 InetAddress.getHostAddress( ) 获取 IP 的文本表示。
但是,查看您的代码,您可以粘贴您收到的错误吗?无论如何,您应该只使用 InetAddress 对象,因此可能会发生其他情况。
Use InetAddress.getHostAddress() to get the textual representation of the IP.
However, looking at your code, can you paste the error that you're getting. You should just be using the InetAddress object anyway, so something else might be happening.
你不需要做任何这些。 DatagramPacket 中已包含源 IP 地址和端口。只需更改数据并发送相同的DatagramPacket。实际上,您的代码可能应该按原样工作 - 如果失败,原因不是 /,这只是显示的内容。
You don't need to do any of that. The DatagramPacket already has the source IP address and port in it. Just alter the data and send the same DatagramPacket. Actually your code should probably work as-is - if it is failing the reason is not the /, that's only what's being displayed.
我已经使用过该程序。当我的 IP 地址是动态时,我无法获取 IP 地址。然后我升级了我的系统IP并为此目的采用了静态IP。
I have used the program. I was not able to get the IP address when my IP address was dynamic. Then I upgraded my system IP and took a static IP for this purpose.