开发者

WINSOCK - Setting a timeout for a connection attempt on a non existing IP?

开发者 https://www.devze.com 2023-03-09 13:31 出处:网络
I am developing a RTSP Source filter in C++, and I am using WINSOCK 2.0 - blocking socket. When I create a blocking socket, I set its SO_RCVTIMEO to 3 secs like so:

I am developing a RTSP Source filter in C++, and I am using WINSOCK 2.0 - blocking socket.

When I create a blocking socket, I set its SO_RCVTIMEO to 3 secs like so:

int ReceiveTimeout = 3000; 
int e = setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&ReceiveTimeout, sizeof(int));

My filter tries to connect to IP_ADDRESS:554 (554 is RTSP server port). If there is a server listening on that IP on the port 554, all goes well, but:

  1. If my filter creates a socket to an existing IP address, but on a random port which no one listens on, connect() waits for 3 secs and returns WSAETIMEDOUT. So after 3 secs, I know that the provided URL is bad.

  2. If my filter creates a socket to a non existing IP address, and tries to connect it, it hangs for about 10 secs before returning SOCKET_ERROR. So, SO_RCVTIMEO gets ignored if the IP doesn't exist on the network...

QUESTION: How can I set the timeout for a non existing IP, in the second case? Do I need to send ICMP PING first to see does the IP exist, or perform some other check like that?

Any help will be appreciated. Thanx. :)

THE ANSWER TO MY PROBLEM

Because I am using blocking sockets, call to connect() blocks, until the connection is made, or the connection fails because the host is not responding, or it is refusing connection. If I set socket's timeout to be 3 seconds, and try to connect to a host that doesn't exist, my pc (client) will send TCP packet with SYN flag set, to initiate the Threeway handshake. Normally, the host, if up, will respond with TCP packet containing ACK and SYN flags set, and then, client (me) would send the TCP packet with ACK flag set. Then the connect开发者_Python百科ion is made. BUT if the host is down, and the SYN is sent, client waits until the 3 second timeout expires, and then tries AGAIN, and AGAIN, until the TcpMaxConnectRetransmissions (MICROSOFT ARTICLE) registry setting is reached, because the host can be UP but the SYN packet might get lost... My Windows XP has this setting at 4, I guess, so each time it tries to send SYN, it waits 3 seconds, and when the fourth try fails, it returns SOCKET_ERROR (after 12 secs), and sets WSAETIMEDOUT as the last WSA error.

The way around this is using non blocking sockets, and trying to manually measure the connection attempt time (because now the connect() wouldn't block) as Martin James suggested.

Another way is to fiddle with the registry, which is the last resort...


Bite the bullet. The remote IP may not be running a PING server or PING may be blocked by some router, so it's no help. Can you not just wait the 10 sec and then make whatever error indication you use?

If you absolutely have to time out the attempted connection after 3 seconds, you can time it out yourself.


Actually, Berkeley sockets have not timeout for connect, so you can not set it. ICMP PING is not helpful, i don't know why, but if host not exists you spend around 1 second with PING. Try use ARP for detect is host exists.


from cmd you can ping the ip with a timeout like this 'ping -w 100 -n 1 192.168.1.1'

it will return within 100mS

you can check the return code by 'echo %errorlevel% 0 = ok, 1 = fail, then you know if you should try connect

in c++

bool pingip_nowait(const char* ipaddr)
{
    DWORD exitCode;

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.hStdOutput =  GetStdHandle(STD_OUTPUT_HANDLE);
    si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
    si.wShowWindow = SW_HIDE;

    CString cmd = "ping -w 100 -n 1 ";
    cmd += ipaddr;
    if (!CreateProcess(NULL,
        cmd.GetBuffer(),
        NULL,
        NULL,
        FALSE,
        0,
        NULL,
        NULL,
        &si,
        &pi)) {
            TRACE("ERROR: Cannot launch child process\n");
            return false;
    }

    // Give the process time to execute and finish
    WaitForSingleObject(pi.hProcess, 200L);

    if (GetExitCodeProcess(pi.hProcess, &exitCode))
    {
        TRACE("ping returned %d\n", exitCode);
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
        return exitCode==0 ? true : false;
    }
    TRACE("GetExitCodeProcess() failed\n");
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return false;
} 
0

精彩评论

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

关注公众号