我已经做了计划,从客户端发送一个UDP数据包到这里服务器发送源代码
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程序就认不出来了,程序只是不断侦听的端口,并没有任何反应?