I am trying out networking in C++ with a simple server and client, starting with some Winsock examples.
Whenever I try to compile the server with Code::Blocks, I get the following errors:
||=== Build: Debug in Server Test (compiler: GNU GCC Compiler) ===|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|12|warning: ignoring #pragma comment [-Wunknown-pragmas]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp||In function 'int main()':|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|47|error: 'getaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|57|warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int' [-Wformat]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|58|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|67|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|73|error: 'freeaddrinfo' was not declared in this scope|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
I have tried the solution from this question, however, this results in 36 errors.
I am having the same issue with the client code.
Server Code:
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
// Echo the buffer back to the sender
iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
In case someone is still struggling with this issue. All you have to do is the following:
#define _WIN32_WINNT 0x501
(used by ws2tcpip.h
)g++ prog.c -o prog -lws2_32
These steps are made on both codes, server and client.
Hope this can help.
I don't have VS on this machine, but try importing winsock2.h before windows.h.
I do have Cygwin with gcc, and compiled the source at the command line:
gcc server.cpp -lws2_32 -oserver.exe
Seems to work:
telnet 127.0.0.1 27015
Hi
Hi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With