Android App: Address Family not Supported by Protocol
I am trying to have my Android application sent Telnet commands over a small network to another device, and whenever I declare the DatagramSocket it throws a SocketException saying: Address Family not Supported by Protocol. Here is my code below:
try {
addr = InetAddress.getByName(ipAddress);
sock = new DatagramSocket(); //SocketException created here
//first message - cmd
length = cmd.le开发者_如何学运维ngth();
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();
}
}
Possible solutions I've considered but haven't made work:
- Emulator needs port redirect through development machine, which ports to use?
- I'm not using correct version of IP4/6, how is this set?
- Device uses TCP protocol, maybe I'm using the wrong socket type?
Other Important Info:
- I've only run this on an Emulator
- Development Machine correctly sent telnet commands from Command Prompt
- Network consists of development machine, router, and device.
UPDATE: 2/9/11
I've changed this code to the following, but I'm still getting an exception:
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();
}
The message from the exception says "Permission Denied." Does this mean that my device is blocking the socket from connecting?
You've coded this the wrong way. Telnet uses TCP which uses stream (connection oriented) sockets, not the datagram sockets used by UDP.
Search for tcp examples.
精彩评论