简体   繁体   English

绑定-地址已在使用中

[英]Bind - Address Already in Use

So i'm setting up a VoIP software and my connection thread to another peer has a UDP socket (to send audio) and a tcp socket for signaling/text. 因此,我正在设置VoIP软件,并且我与另一个对等方的连接线程具有UDP套接字(用于发送音频)和用于信令/文本的tcp套接字。 Now, to bypass firewalls and NAT, i need to use a TCP Hole punching technique, meaning i need to try and connect from this side of the call, which will open up holes in the firewall and NAT, and then i await a connection on the same port and the other peer should be able to connect to me. 现在,要绕过防火墙和NAT,我需要使用TCP穿孔技术,这意味着我需要尝试从呼叫的这一端进行连接,这将在防火墙和NAT中打开漏洞,然后等待连接同一端口和另一个对等方应该可以连接到我。

    try {
        // UDP Socket
        DatagramSocket callSocket = new DatagramSocket(null);
        callSocket.setReuseAddress(true);
        callSocket.bind(new InetSocketAddress(myIP, 0));

        //Send/receive a few packets on callSocket

        // Addresses to use
        InetSocketAddress myAddress = new InetSocketAddress(myIP, callSocket.getLocalPort());
        InetSocketAddress targetAddress = new InetSocketAddress(InetAddress.getByName("192.168.1.1"), 6800);

        // TCP Hole Punch
        Socket tcpSocket = new Socket();
        tcpSocket.setReuseAddress(true);
        tcpSocket.bind(myAddress);
        try {
            tcpSocket.connect(targetAddress, 50);
            // this never connects. it's just meant to open a hole in a firewall
        } catch (SocketTimeoutException ignore) {
            System.out.println("Timeout!");
        }
        tcpSocket.close();

        // Open up TCP socket
        ServerSocket tcpTempSocket = new ServerSocket();
        tcpTempSocket.setReuseAddress(true);
        tcpTempSocket.bind(myAddress);

        // accept connection and do stuff
    } catch (Exception ex) {
        ex.printStackTrace();
    }

When i get to the 3rd and final bind, i get the "Bind Exception: Address already in use". 当我到达第三个也是最后一个绑定时,我得到了“绑定例外:地址已在使用中”。 I googled it up and read that the previous socket could still be hanging on something and wasn't closing, etc. 我在Google上搜索了一下,并阅读到以前的套接字仍然可以挂在某个东西上并且没有关闭,等等。

EDIT: this only happens in some computers. 编辑:这仅在某些计算机上发生。 On others, it binds everything without an issue 在其他方面,它可以毫无问题地绑定所有内容

EDIT: using "netstat", i can see that the connection is being hung up on "SYN_SENT" state on the computer where the bind goes wrong 编辑:使用“ netstat”,我可以看到连接被绑定错误的计算机上挂在“ SYN_SENT”状态

Anyone have any tips on why this happens or how to i go around it? 任何人都有关于为什么发生这种情况或如何解决它的任何提示?

Ok, so. 好吧 The answer is... You can't go around it. 答案是……您无法解决它。 This is an OS feature, which makes tcp connections get hung up on this port. 这是一项操作系统功能,使tcp连接挂在该端口上。 On some computers it may take 5seconds to clear. 在某些计算机上,可能需要5秒钟才能清除。 On others, it may take over 2minutes. 在其他情况下,可能需要2分钟以上的时间。

Now, what i've done to get around this was... well, the only thing i could think of. 现在,我为解决这个问题所做的就是……好吧,我唯一想到的就是。 When the program starts, it checks whether it supports tcp hole punching, or other ways of bypassing firewalls. 程序启动时,将检查它是否支持tcp打孔或其他绕过防火墙的方法。 The peers, when establishing the call, will trade parameters based on what they can do and, from a given priority list, choose a method they both support. 在建立呼叫时,对等方将根据他们可以做什么来交换参数,并从给定的优先级列表中选择它们都支持的方法。

In my case, on my computer, TCP Hole Punch works. 就我而言,在我的计算机上,TCP Hole Punch可以工作。 In my mum's laptop it doesn't, and i resorted to other techniques (UDP Hole Punch, UPnP, SOCKS, etc etc) 在我妈妈的笔记本电脑中却没有,我求助于其他技术(UDP打孔,UPnP,袜子等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM