Search code examples
c++socketswinsock

Winsock connect() hanging on one network, but not another


I am having trouble using the connect() function. My code was completely working before, but now I have moved to a different physical network and my blocking call to connect() no longer works, and just seems to hang indefinitely. Receiving broadcasts over UDP still works fine. Going back to the old network it works fine again. I cant for the life of me figure out why it works on one network and not the other. I have checked firewall settings and they are correct. What could be going on?

I have a pre-defined port being used and I am getting the address from the broadcast. I use recievefrom to receive the broadcast and set the outgoing ip address from it ret = recvfrom (bcast, bcast_read,sizeof(j4cDAC_broadcast),0,(sockaddr*)&from,&size);

to.sin_addr = from.sin_addr;

local.sin_addr.s_addr = inet_addr("0.0.0.0");

Then for the TCP connection I have

dac = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    //  cout << "SOCKET\n";
        if (dac == INVALID_SOCKET)
            {
            SetConsoleTextAttribute(console,(WORD)12);
            cout << "TCP socket failed: " << WSAGetLastError();
            connected_ = false;
            return(1);
            }

        //set SO_REUSEADDR on a socket to true (1):
        bool optval = true;

        setsockopt(dac, SOL_SOCKET,SO_DONTLINGER,(const char*)&optval, sizeof(optval)); 

        int pies = setsockopt(dac, SOL_SOCKET,SO_REUSEADDR,(const char*)&optval, sizeof(optval) );  

        if (pies == SOCKET_ERROR )
            {
            SetConsoleTextAttribute(console,(WORD)12);
            cout << "SETSOCKOPT ERROR: " << WSAGetLastError() << endl;
            }    // */
        local_T = local;
        local_T.sin_port =  htons ((short)TCPport);

        //bind the tcp socket
        bndt = bind(dac,(SOCKADDR*) &local_T,sizeof(local_T) );

        if (bndt == SOCKET_ERROR )
            {
            SetConsoleTextAttribute(console,(WORD)12);
            cout << "BIND TCP FAILED: " << WSAGetLastError();

            if (WSAGetLastError() == WSAEACCES)
                cout << "ACCESS DENIED";

            cout << endl;
            SetConsoleTextAttribute(console,(WORD)7);
            shutdown(dac,2);
            closesocket(dac);

            connected_ = false;
            return 1;
            }

        c = connect(dac, (sockaddr*) &to, size);      // <------- This hangs

        if (c == SOCKET_ERROR)
            {
            cout << "connection problem: " << WSAGetLastError() <<endl;
            }

        connected_ = true;`

Solution

  • I found this to be an issue with VMWare virtual networking devices. Even though I had no virtual machines running, after much testing of various things, I found the device broadcast was being received on one of the virtual networking interfaces from VMWare somehow. Disabling these two devices has fixed this issue.