저는 C++을 배웠습니다. 단지 텍스트 메시지 만 출력하는 매우 간단한 HTTP 서버를 구현하려고했습니다. 나는 내가 갖는 마이크로 소프트 비주얼 스튜디오에게 2005C++ - A 매우 간단한 HTTP 서버 : WSA unreferenced error
를 사용 행 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;
}
감사합니다.
두 빌드에서 모두 컴파일해야합니다. 단언에서 수행하는 것은 모두 호출 함수입니다. 릴리스 빌드는 분명히 실행될 때 유용하지 않습니다. –