sockets - 安卓应用 : Address Family not Supported by Protocol

标签 sockets android-emulator

我试图让我的 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();
    }
}

我考虑过但没有奏效的可能解决方案:
  • 模拟器需要通过开发机重定向端口,使用哪些端口?
  • 我没有使用正确版本的 IP4/6,这是如何设置的?
  • 设备使用 TCP 协议(protocol),也许我使用了错误的套接字类型?

  • 其他重要信息:
  • 我只在模拟器上运行过这个
  • 开发机从命令提示符
  • 正确发送了 telnet 命令
  • 网络由开发机、路由器和设备组成。

  • 更新: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/

    相关文章:

    java - 发送 C# 和 Java 的套接字数据 - 总体和具体

    php - 当使用 netcat 作为客户端发送文件或管道输出时,如何避免 netcat 阻塞?

    android - 模拟器不返回 320x240 分辨率的实际宽度和高度

    Java Android 模拟器 : Crash when using Maps v2 (Caused by android. view.InflateException:二进制 XML 文件行 #11:错误膨胀类 fragment

    android - Eclipse 不会将 BLU Dash 3.5 识别为 Activity 的 Android 设备

    sockets - window 电话 7 : Socket Programming

    java - 当我从客户端关闭套接字时,socket.getInputStream.read() 不会抛出异常

    algorithm - 处理来自套接字的碎片化输入的一般方法

    java - 安卓模拟器 : Unfortunately App has stopped

    android - 自动填充安卓收件箱