如何修复getsockname失败:EBADF(错误的文件号)?

时间:2014-01-24 05:20:52

标签: java android network-programming

我的应用程序执行network operations,如socket创建,发送和接收TCP数据包。有时我会得到以下异常,我无法解决。我尝试了所有可能的修复,但因为它是致命的例外我的应用程序将崩溃和关闭,这是非常烦人的。

我们可以做些什么来避免这个问题或至少可以避免崩溃?

任何帮助都将受到高度赞赏!!

01-24 10:29:27.675: E/AndroidRuntime(3781): java.lang.AssertionError: libcore.io.ErrnoException: getsockname failed: EBADF (Bad file number)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at libcore.io.IoBridge.getSocketLocalAddress(IoBridge.java:583)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at java.net.Socket.cacheLocalAddress(Socket.java:950)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at java.net.Socket.startupSocket(Socket.java:568)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at java.net.Socket.tryAllAddresses(Socket.java:127)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at java.net.Socket.<init>(Socket.java:177)
01-24 10:29:27.675: E/AndroidRuntime(3781):     at java.net.Socket.<init>(Socket.java:149)

PS:这是我的示例代码:

InetAddress local_inet_addr = null;
    try {
        Socket socket = new Socket("www.xxxxxxx.com", 80);
        if (socket != null) {
            local_inet_addr = socket.getLocalAddress();
            socket.close();
            socket = null;
        }
    } catch (Exception e) {
        try {
            Socket socket = new Socket("www.yyyyyyyy.net", 80);
            if (socket != null) {
                local_inet_addr = socket.getLocalAddress();
                socket.close();
                socket = null;
            }
        } catch (Exception ex) {
        }
    }

这里xxxx和yyyy将是我的个人域名。

0 个答案:

没有答案