CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Windows Socket makes app crash if no internet connection

    Hello
    I'm strugling to understand why my app is crashing if there is no internet connection.
    Code:
    int main()
    {
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
    		std::cout << "WSAStartup failed.\n";
    		return 1;
    	}
    	SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	struct hostent *host;
    	host = gethostbyname("myhost");
    	SOCKADDR_IN SockAddr;
    	SockAddr.sin_port = htons(80);
    	SockAddr.sin_family = AF_INET;
    	SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    	if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
    		std::cout << "Could not connect";
    		return 1;
    	}
    	char comanda[250];
    	sprintf(comanda, "GET /test.php?what=%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", "hello", "myhost.com");
    
    	send(Socket, comanda, strlen(comanda), 0);
    	closesocket(Socket);
    	WSACleanup();
    	Sleep(1000);
    	return true;
    }
    Debug crash code:
    Code:
    First-chance exception at 0x00161831 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000C.
    Unhandled exception at 0x00161831 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000C.
    It works well as long as the computer is connected to internet but if i disconect from internet the application crash but don't understand why. Any idea ?
    Thanks very much!
    Last edited by eclessiastes; October 18th, 2014 at 12:46 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Windows Socket makes app crash if no internet connection

    Did you debug your code?
    What line causes the exception?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Windows Socket makes app crash if no internet connection

    You are not checking the return value of socket(), gethostbyname() and send() for errors. The return value of ALL functions should always be checked for error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: Windows Socket makes app crash if no internet connection

    Quote Originally Posted by 2kaud View Post
    You are not checking the return value of socket(), gethostbyname() and send() for errors. The return value of ALL functions should always be checked for error.
    I tried your way:
    Code:
    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) != 0) {
    		return 1;
    	}
    Now it works but i don't understand why the app crash if i don't check the values of returns.
    Anyway, thanks for help, you saved my day!

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Windows Socket makes app crash if no internet connection

    Now it works but i don't understand why the app crash if i don't check the values of returns.
    In this case because you are passing an invalid socket to send().

    Also a better way to test would be
    Code:
    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (Socket == INVALID_SOCKET)......
    as this is what is specified in the documentation. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured