【发布时间】:2022-01-16 15:06:37
【问题描述】:
我正在尝试在我的 Android 应用程序和本地 Apache 服务器之间建立套接字连接。但是,当代码运行时,我在 Logcat 中收到以下错误:java.net.BindException: bind failed: EACCES (Permission denied)。这是我的客户端代码(错误发生在socket = new DatagramSocket(this.serverPort, ip);行中:
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(TAG, ip.getHostAddress());
}
catch(UnknownHostException uhe) {
System.out.println("Unknown host");
ipAddressAbleToBeFound = false;
}
if (ipAddressAbleToBeFound) {
try {
socket = new DatagramSocket(this.serverPort, 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, this.serverPort);
try {
socket.send(packet);
}
catch(IOException ioe) {
System.out.println("I/O error occurred");
}
}
}
}
}
}
我认为我的 Android 清单文件具有适当的权限。我的 ip 设置为我的本地 IP 地址,这是我的本地 Apache 服务器被访问的方式。我的this.serverPort 设置为80,小于1024。我读到问题可能是因为我试图连接到小于1024 的端口。但是,当我将this.serverPort 设置为1024 时,我收到以下错误:java.net.BindException:绑定失败:EADDRNOTAVAIL(无法分配请求的地址)。所以我不确定要尝试什么。如果我需要提供任何其他信息,请告诉我!谢谢!
【问题讨论】:
-
ip的使用值是多少? -
ip的值是我的本地ip地址 -
请说出我问的值。
-
String target是否已经包含此地址? -
是的,我想我可能已经找到了解决这个问题的方法,尽管我即将发布
标签: java android sockets networking