我正在學習C++,我想嘗試實現一個非常簡單的HTTP服務器,它將只輸出一條文本消息。我使用Microsoft Visual Studio 2005.C++ - 一個非常簡單的HTTP服務器:WSA未引用的錯誤
我得到了: 行20:警告'wsa'未引用的本地變量,而我試圖編譯我的源代碼。我錯過了什麼嗎?
這是我的源代碼。
#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;
}
非常感謝。
它應該在兩個版本中編譯,因爲他在斷言中所做的全部都是調用函數。然而,發佈版本在運行時顯然不會有用。 – 2010-05-11 11:39:30