2

I am trying to implement a sample code with Websocket client/server:

  • works on both windows and linux
  • supports multiple clients
  • no extra dependencies, pure C/C++ easy to use code

For example, implementing something like a chat room that accepts many users, would be a great base for me to understand how Websockets work and use them in my project.

I am looking at this website and I see this example of Websockets in C++.

I am using Clion IDE which uses MinGW compiler and these headers do not exist:

#include <netinet/in.h>
#include <netdb.h>

Auto-code-completion suggests these headers:

#include <winsock.h>
or    
#include <winsock2.h>

Even though I see no errors on my IDE with those headers, when I run the code I get linking errors:

CMakeFiles\server_client.dir/objects.a(server.cpp.obj): In function `main':
C:/Users/Shiro/ClionProjects/server_client/server.cpp:44: undefined reference to `listen@8'
C:/Users/Shiro/ClionProjects/server_client/server.cpp:58: undefined reference to `select@20'
C:/Users/Shiro/ClionProjects/server_client/server.cpp:66: undefined reference to `__WSAFDIsSet@8'
collect2.exe: error: ld returned 1 exit status

On a sidenote, is this code even good to implement a Websocket client/server in C++ ? I want this to work on both windows and linux and have no external dependencies. Just pure C/C++. A couple of libraries I saw (like libwebsockets) were way too confusing and over-complicated to me, and they lack of any documentation or any examples. I did not understand a single line of code. Websockets in other languages like Java or C# are way more straightforward. Can someone point me in the right direction please ?

dimitris93
  • 4,155
  • 11
  • 50
  • 86

1 Answers1

0

The windows socket API is very similar to the POSIX one, but there are some differences, as explained in this MSDN article, and the fact that you have to call WSAStartup() before using this API.

About the header take the winsock2.h. But you shouldn't forget to include the socket library in your project (either makefile/project parameters or as #pragma comment(lib, "Ws2_32.lib").

If you want portable code, you may want to consider boost::asio

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Getting `undefined reference to WSAStartup`. What is `"Ws2_32.lib"` supposed to be ? Do I need to put a path or something ? – dimitris93 Apr 08 '16 at 23:13
  • It's supposed to refer to the dll stub for ws2_32.dll which is in system32 (no need for an additional path). To lib should however be in one of the path given to the linker (e.g. LIB environmnet variable). – Christophe Apr 08 '16 at 23:28
  • I don't understand why I keep getting those `undefined reference` errors – dimitris93 Apr 08 '16 at 23:29
  • I am having trouble installing boost with Clion. I have posted a [question](http://stackoverflow.com/questions/36519453/setup-boost-in-clion) here if you could help me – dimitris93 Apr 09 '16 at 16:52
  • Now the problem has been narrowed down to this [question](http://stackoverflow.com/questions/36520283/how-to-link-ws2-32-in-clion) if you could help. I don't understand how to link the `ws2_32` library . The `#pragma` is MSVC only keyword – dimitris93 Apr 09 '16 at 18:04
  • ok ! Much better then. Ws2_32 is a dll that belongs to the windows system. In order to tell this to the linker, you need to link your code together with ws2_32.lib. The pragma is compiler specific. If this does'nt work for you, you could add the library to the compiling options (e.g. try -lws2_32 or manually add the path to the windows development kit). – Christophe Apr 09 '16 at 20:30