开发者

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:

  1. Emulator needs port redirect through development machine, which ports to use?
  2. I'm not using correct version of IP4/6, how is this set?
  3. Device uses TCP protocol, maybe I'm using the wrong socket type?

Other Important Info:

  1. I've only run this on an Emulator
  2. Development Machine correctly sent telnet commands from Command Prompt
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜