when I compile this program and run it, I get the output below. I added the bind failed with error output for debugging and it suggests that with the error code that I received from listen the socket is not bound using bind but the 0 output is confusing me as that is the output when everything worked. I would appreciate any suggestions
#define DefaultBufLen 1024
SOCKET Socket = INVALID_SOCKET;
int difficulty = 0;
WSADATA wsaData;
struct addrinfo* result = NULL,
* ptr = NULL,
hints;
sockaddr_in Service;
int recvbuflen = DefaultBufLen;
char SendBuf[DefaultBufLen] = "";
char RecvBuf[DefaultBufLen] = "";
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
wprintf(L"Startup failed with error code %d\n", WSAGetLastError());
return 1;
};
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET) {
wprintf(L"Socket failed with error: %1d\n", WSAGetLastError());
WSACleanup();
return 1;
};
Service.sin_family = AF_INET;
Service.sin_addr.s_addr = inet_addr("127.0.0.1");
Service.sin_port = htons(27015);
bind(Socket, (SOCKADDR*)&Service, sizeof(Service));
wprintf(L"bind failed with error: %ld\n", WSAGetLastError());
while (1) {
if (listen(Socket, 2000) == SOCKET_ERROR) {
wprintf(L"listen failed with error: %ld\n", WSAGetLastError());
closesocket(Socket);
WSACleanup();
return 1;
}
else {
wprintf(L"listen succeeded!\n");
}
SOCKET AcceptingSocket;
wprintf(L"Waiting for client to connect...\n");
AcceptingSocket = accept(Socket, NULL, NULL);
if (AcceptingSocket == INVALID_SOCKET) {
wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
closesocket(Socket);
WSACleanup();
return 1;
}
else {
wprintf(L"Client connected.\n");
}
int Error = 0;
do {
Error = recv(Socket, RecvBuf, recvbuflen, 0);
if (Error > 0)
printf("Bytes received: %d\n", Error);
else if (Error == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while (Error > 0);
string TranslatedRecvBuf = RecvBuf;
vector<string> Request;
boost::split(Request, TranslatedRecvBuf, boost::is_any_of(";"));
if (Request[0] == "RBlock") {
Irrelevant
else if (Request[0] == "DBlock") {
Irrelevant
else if (Request[0] == "TxIncl") {
Irrelevant
else if (Request[0] == "BaLook") {
Irrelevant
closesocket(AcceptingSocket);
};
};
Output:
bind failed with error: 0
listen failed with error: 10022
[process exited with code 1 (0x00000001)]