Reputation: 13
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.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();
}
}
Possible solutions I've considered but haven't made work:
Other Important Info:
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?
Upvotes: 1
Views: 7514
Reputation: 40367
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.
Upvotes: 3