2

I am a C++ noob.

I want to use Winsock to send a HTTP request to an IP address. I found a lot of content online, which is great. All of the example had the same errors, I realized that they all require Winsock.

I never used a library in C++, I don't know what or how to link anything to my project (in Visual Studio), or what I need to download.

It specifically does not like the following lines of code

#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr
#include<netdb.h> //hostent

The Microsoft guide does not tell me how to fix or link the library to my project.

How do I link and use Winsock?

Thank You!

user2990508
  • 255
  • 1
  • 4
  • 11
  • 2
    What is wrong with [this](https://msdn.microsoft.com/en-us/library/windows/desktop/ms740632(v=vs.85).aspx)? Can you ask a *specific* question that is not answered on an easily found webpage? – abelenky Oct 30 '15 at 17:51
  • Requiring an OS network stack is not an error:) – Martin James Oct 30 '15 at 17:54
  • Three steps to happiness: 1. Locate the required function's documentation on MSDN. 2. Scroll down to find out which headers to include. 3. Right next to it, find the libraries to link with. Using these three steps also works for WinSock. Note that WinSock and the Berkeley Socket API have a large overlap, but they are still significantly distinct. I'd also suggest you don't use low-level sockets but instead use something like e.g. ZeroMQ or even more abstract RPC-like networking layers. Those are initially a bit more complex to setup, but it's worth it. – Ulrich Eckhardt Oct 30 '15 at 18:29
  • Most of the stuff you'll be looking for is in winsock2.h or ws2tcpip.h. Include them. Remember to link in ws2_32.lib, and don't expect a program written for Linux to compile and run anywhere but Linux. In addition, [read up on WSAStartup](https://msdn.microsoft.com/en-us/library/windows/desktop/ms742213(v=vs.85).aspx) and [also on WSACleanup](https://msdn.microsoft.com/en-ca/library/windows/desktop/ms741549(v=vs.85).aspx) You'll need both. – user4581301 Oct 30 '15 at 18:49
  • I'd recommend using the sockets from the boost library. they work on more than Windows. you won't have to learn how to use sockets twice the day you'll need to switch to a new platform. – J-Mik Oct 30 '15 at 20:14
  • Also keep in mind the order of header inclusion for `winsock2.h`: [C++ Redefinition Header Files (winsock2.h)](http://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h) – MicroVirus Oct 30 '15 at 20:28

2 Answers2

4

To link your app successfully, instead of going "Options..." you can put the following lines into your stdafx.h header:

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#pragma comment(lib, "WS2_32.lib")

The #pragma comment tells the linker to use WS2_32.lib library that you need.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Sergio
  • 41
  • 2
  • This looks like an answer to me, no reason for it to be a comment. – Brian Ray Nov 05 '15 at 14:54
  • @Brian Ray because the question I answered is in the comments. But you can vote for my answer if you like, so that I earn points and be able to comment :) – Sergio Nov 05 '15 at 15:07
2

It specifically does not like the following lines of code

#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr
#include<netdb.h> //hostent

Those headers are not available on Windows, they are used on other platforms only.

How do I link and use Winsock?

On Windows, you need to use winsock.h or winsock2.h in your code, and link to ws2_32.lib.

And you need to call WSAStartup() and WSACleanup(), which do not exist on other platforms.

And you need to use SOCKET instead of int for socket handles. On Windows, sockets are true kernel objects. On other platforms, they are represented as file descriptors.

And you need to call closesocket() instead of close() when disconnecting/freeing a socket handle.

Beyond that, most other BSD-based socket API function calls (socket(), bind(), connect(), recv(), send(), select(), etc) work the same in Windows as in other platforms (well, select() has one minor difference - the first parameter is ignored). And, of course, WinSock has many Windows-specific extensions that other platforms do not have. But if you are trying to use someone else's code that you found online, chances are it is using the BSD-compatible functions only.

That being said, Winsock is a lower-level networking API and very difficult for noobs to use correctly. And then you have to add an application-layer protocol, like HTTP, on top of it, which is even harder for noobs to get right. Wanting to learn something new is great, but jumping right into HTTP without understanding the fundamentals first is a recipe for disaster. You are better off using Microsoft's WinInet or WinHTTP API instead, let it handle all of the networking and protocol details for you.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thanks a ton, but one question, how do I `link to ws2_32.lib` – user2990508 Oct 31 '15 at 00:29
  • Read the Visual Studio documentation: [.Lib Files as Linker Input](https://msdn.microsoft.com/en-us/library/ba1z7822.aspx) – Remy Lebeau Oct 31 '15 at 00:49
  • Thanks for clearing things up for me, but last thing (this sounds stupid because it is), where do I get `ws2_32.lib` I can't find a download link... I will mark the answer as accepted. – user2990508 Oct 31 '15 at 01:12
  • It should be included with Visual Studio. You don't need to specify the full path, just add `ws2_32.lib` by itself in the linker's `Additional Dependencies` list and let the linker figure it out during compiling. – Remy Lebeau Oct 31 '15 at 01:25