我试图让我的 Android 应用程序通过小型网络将 Telnet 命令发送到另一台设备,每当我声明 DatagramSocket 时,它都会抛出一个 SocketException 说:协议(protocol)不支持的地址系列 .下面是我的代码:
try {
addr = InetAddress.getByName(ipAddress);
sock = new DatagramSocket(); //SocketException created here
//first message - cmd
length = cmd.length();
message = cmd.getBytes();
packet = new DatagramPacket(message, length, addr, portAddr);
sock.send(packet);
//second message - highCMD
length = highCMD.length();
message = highCMD.getBytes();
packet = new DatagramPacket(message, length, addr, portAddr);
sock.send(packet);
sock.close();
} catch (SocketException e) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(v.getContext()).create();
alertDialog.setTitle("Send High CMD Error!");
alertDialog.setMessage("SocketException");
alertDialog.show();
} catch (IOException e){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(v.getContext()).create();
alertDialog.setTitle("Send High CMD Error!");
alertDialog.setMessage("IOException");
alertDialog.show();
}
}
我考虑过但没有奏效的可能解决方案:
其他重要信息:
更新:2/9/11
我已将此代码更改为以下代码,但仍然出现异常:
try {
addr = InetAddress.getByName(ipAddress);
socketAddress = new InetSocketAddress(addr, portAddr);
sock = new Socket();
sock.connect(socketAddress);
sock.close();
} catch (SocketException e) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(v.getContext()).create();
alertDialog.setTitle("Send High CMD Error!");
alertDialog.setMessage("SocketException" + e.getMessage());
alertDialog.show();
}
来自异常的消息显示“权限被拒绝”。这是否意味着我的设备正在阻止套接字连接?
最佳答案
您以错误的方式对此进行了编码。 Telnet 使用 TCP,它使用流(面向连接)套接字,而不是 UDP 使用的数据报套接字。
搜索 tcp 示例。
关于sockets - 安卓应用 : Address Family not Supported by Protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4939237/