Hi everyone,
I started following beej's guide to network proggraming (here : http://beej.us/guide/bgnet/output/html/multipage/index.html).
In the intro section, he gives a piece of code that you have to put inorder to be able to use the socket librarie. I put that piece of code in a pretty much plain programm but I get this error : 'stderr' was not declared in this scope
I get it at line 15.
Here is my code, I'm using codeblock IDE :
As Homberto's link says, you should be using winsock 2 rather than winsock 1. That means using the right header file and linking to the right lib. Check out the link for details.
Incidentally, as you're including <iostream>, rather than including <cstdio> to fix the "'stderr' was not declared in this scope" error you should be using the C++ IO Streams equivalent: cerr.
std::cerr << "WSAStartup failed.\n"; // write to error stream
I would also just use return to exit main, rather than exit(), which can be problematic in C++ code.
#include <iostream>
#include <winsock2.h> // now the newer header
// get compiler to tell the linker what lib you need to link with
// (or add lib to linker options)
#pragma comment(lib, "ws2_32.lib")
usingnamespace std;
int main()
{
WSADATA wsaData = {0}; // it is all CAPS!
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
cerr << "WSAStartup failed.\n"; // use error stream
return 1; // not exit()
}
cout << "WSAStartup succeeded.\n";
WSACleanup();
return 0;
}
Hi,
Thanks for your answers. I tried your code andy but it still gave me the same errors but at line 16 and 22. Then I tried linking to "ws2_32.lib" but now it gives me this error : Cannot find -ws2_32.lib.
Do i need to put a copy of the lib in my project file ? And if so please tell me where I can find that file.