⑴ 请教大神,怎么使用java实现UDP端口扫描
给你个UDP服务端与客户端的示例:
服务端代码:
importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;
publicclassUDPMulticastServer{
finalstaticintRECEIVE_LENGTH=1024;
staticStringmulticastHost="224.0.0.1";
staticintlocalPort=9998;
publicstaticvoidmain(String[]args)throwsException{
InetAddressreceiveAddress=InetAddress.getByName(multicastHost);
if(!receiveAddress.isMulticastAddress()){//测试是否为多播地址
thrownewException("请使用多播地址");
}
intport=localPort;
=newMulticastSocket(port);
receiveMulticast.joinGroup(receiveAddress);
booleanisStop=false;
while(!isStop){
DatagramPacketdp=newDatagramPacket(newbyte[RECEIVE_LENGTH],RECEIVE_LENGTH);
receiveMulticast.receive(dp);
Stringdata=newString(dp.getData()).trim();
System.out.println(data);
if("exit".equals(data)){
System.out.println("程序退出");
isStop=true;
}
}
receiveMulticast.close();
}
}
客户端代码:
importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;
publicclassUDPMulticastClient{
staticStringdestAddressStr="224.0.0.1";
staticintdestPortInt=9998;
staticintTTLTime=4;
publicstaticvoidmain(String[]args)throwsException{ InetAddressdestAddress=InetAddress.getByName(destAddressStr);
if(!destAddress.isMulticastAddress()){//检测该地址是否是多播地址
thrownewException("地址不是多播地址");
}
intdestPort=destPortInt;
MulticastSocketmultiSocket=newMulticastSocket();
// intTTL=TTLTime;
// multiSocket.setTimeToLive(TTL);
byte[]sendMSG="exit".getBytes();
DatagramPacketdp=newDatagramPacket(sendMSG,sendMSG.length,destAddress,destPort);
multiSocket.send(dp);
multiSocket.close();
}
}
⑵ java怎么实现JSON打包UDP
java实现JSON打包UDP cJSON支持在C程序中创建和解析JSON数据,其提供多种方法供C程序使用,最直接的是将cJSON.c和cJSON.h加入到C工程中
(1) QJsonObject用于在Qt中创建JSON对象
(2)数据传输通过UDP运行