开发者

C++ - A Very simple HTTP server: WSA unreferenced error

开发者 https://www.devze.com 2022-12-30 02:03 出处:网络
I\'m learning C++, i wanted to try implementing a very simple HTTP server that will just output a text message. I use Microsoft Visual Studio 2005.

I'm learning C++, i wanted to try implementing a very simple HTTP server that will just output a text message. I use Microsoft Visual Studio 2005.

I'm getting: Line 20: Warning 'wsa' unreferenced local variable, while i'm trying to compile my source code. Am i missing something?

Here's my source code.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <cassert>

const char html[] = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-type: text/html\r\n"
"\r\n"
"<html>\r\n"
"<head>\r\n"
"<title>Hello, world!</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h1>Hello, world!</h1>\r\n"
"</body>\r\n"
"</html>\r\n\r\n";

int main() {
    WSADATA wsa;

    assert( WSAStartup( MAKEWORD( 2, 2 ), &wsa ) == 0 );

    addrinfo *res = NULL;
    addrinfo hints;

    ZeroMemory( &hints, sizeof( hints ) );

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    assert( getaddrinfo( NULL, "80", &hints, &res ) == 0 );

    SOCKET s = socket( res->ai_family, res->ai_socktype, res->ai_protocol );

    assert( s != INVALID_SOCKET );
    assert( bind( s, res->ai_addr, (int)res->ai_addrlen ) != SOCKET_ERROR );
    assert( listen( s, SOMAXCONN ) != SOCKET_ERROR );

    SOCKET client = accept( s, NULL, NULL );

    assert( client != INVALID_SOCKET );

    char buffer[512];
    int bytes;

    bytes = recv( client, buffer开发者_运维知识库, 512, 0 );

    for ( int i = 0; i < bytes; ++i ) {
        std::cout << buffer[i];
    }

    assert( send( client, html, strlen( html ) - 1, 0 ) > 0 );
    assert( shutdown( client, SD_BOTH ) != SOCKET_ERROR );

    closesocket( client );
    WSACleanup();

    return 0;
}

Many thanks.


If for some reason visual studio 2005 is setting NDEBUG, asserts will be preprocessored out and will not be compiled. This usually happens if compiling in release mode. Try moving the actual code outside of the asserts and just using them to check the return values.

MSDN Assertions page has more information on assert in VS.


assert is a conditional macro, which is defined something like this in Microsoft Libraries:

#ifdef NDEBUG
#define assert(_Expression)     ((void)0)   // assert (something); becomes 0; if NDEBUG is not defined!
#else
... code to show an error
#endif

So all the code you've put inside asserts will not be present when NDEBUG is not defined.

The purpose of NDEBUG is put in checks which will run in debug mode only and not for checking errors.

The code you've written will compile and run in debug build in Visual Studio, but will fail in Release builds.


The code compiles for me once I remove getaddrinfo(), but that isn't your problem. Also, you are severely misusing assert() - it's not supposed to be a general purpose error handling scheme.

0

精彩评论

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

关注公众号