使用Java发送和接收UDP数据包? [英] sending and receiving UDP packets using Java?
问题描述
我已经制作了一个程序,用于将UDP数据包从客户端发送到服务器,这里是发送器代码
I have made a program to send an UDP packets from a client to a server here is the transmitter code
import java.io.IOException;
import java.net.*;
/**
*
* @author hp
*/
public class JavaApplication9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
// TODO code application logic here
byte[] buffer = {10,23,12,31,43,32,24};
byte [] IP={-64,-88,1,106};
InetAddress address = InetAddress.getByAddress(IP);
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, address, 57
);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}
收件人代码函数是
public void run(){
try{
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];
byte[] sendData = new byte[8];
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();
String sendString = "polo";
sendData = sendString.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}catch (Exception e){
}
}
我使用过WireShark程序。 UDP数据包是在接收器的WireShark程序中接收的,但Java程序无法识别它,程序只是一直在监听端口而没有任何反应?
I have used the WireShark program . The UDP packet is received in the WireShark program at the receiver but the Java program wouldn't recognize it , the program just keeps listening to the port and nothing happens ?
推荐答案
接收方必须设置接收方端口以匹配发送方DatagramPacket中设置的端口。对于调试,尝试侦听端口> 1024(例如8000或9000)。端口< 1024通常由系统服务使用,需要管理员访问才能在这样的端口上绑定。
The receiver must set port of receiver to match port set in sender DatagramPacket. For debugging try listening on port > 1024 (e.g. 8000 or 9000). Ports < 1024 are typically used by system services and need admin access to bind on such a port.
如果接收方将数据包发送到它正在侦听的硬编码端口(例如端口) 57)并且发送者在同一台机器上然后你会创建一个到接收器本身的环回。始终使用数据包中指定的端口,如果是生产软件,在任何情况下都需要检查以防止出现这种情况。
If the receiver sends packet to the hard-coded port it's listening to (e.g. port 57) and the sender is on the same machine then you would create a loopback to the receiver itself. Always use the port specified from the packet and in case of production software would need a check in any case to prevent such a case.
数据包无法获得的另一个原因到目的地是发件人中指定的错误IP地址。与TCP不同的UDP将尝试发送数据包,即使地址无法访问且发送方也不会收到错误指示。您可以通过在接收器中打印地址来检查这一点,作为调试的预防措施。
Another reason a packet won't get to destination is the wrong IP address specified in the sender. UDP unlike TCP will attempt to send out a packet even if the address is unreachable and the sender will not receive an error indication. You can check this by printing the address in the receiver as a precaution for debugging.
在您设置的发件人中:
byte [] IP= { (byte)192, (byte)168, 1, 106 };
InetAddress address = InetAddress.getByAddress(IP);
但使用字符串形式的地址可能更简单:
but might be simpler to use the address in string form:
InetAddress address = InetAddress.getByName("192.168.1.106");
换句话说,您将target设置为192.168.1.106。如果这不是接收者,那么你将无法得到数据包。
In other words, you set target as 192.168.1.106. If this is not the receiver then you won't get the packet.
这是一个简单的UDP接收器,可以工作:
Here's a simple UDP Receiver that works :
import java.io.IOException;
import java.net.*;
public class Receiver {
public static void main(String[] args) {
int port = args.length == 0 ? 57 : Integer.parseInt(args[0]);
new Receiver().run(port);
}
public void run(int port) {
try {
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];
String sendString = "polo";
byte[] sendData = sendString.getBytes("UTF-8");
System.out.printf("Listening on udp:%s:%d%n",
InetAddress.getLocalHost().getHostAddress(), port);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
while(true)
{
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData(), 0,
receivePacket.getLength() );
System.out.println("RECEIVED: " + sentence);
// now send acknowledgement packet back to sender
InetAddress IPAddress = receivePacket.getAddress();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, receivePacket.getPort());
serverSocket.send(sendPacket);
}
} catch (IOException e) {
System.out.println(e);
}
// should close serverSocket in finally block
}
}
这篇关于使用Java发送和接收UDP数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!