Download

#include <winsock2.h> not working?

Hi, I’m trying to implement a TCP server based on functions in “winsock2.h”

However, whenever I try to build a solution after adding that library, UE4.24.1 gives me the following error


1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winnt.h(603): warning C4005: 'TEXT': macro redefinition
1>  C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\HAL/Platform.h(996): note: see previous definition of 'TEXT'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10229): error C4668: '_WIN32_WINNT_WIN10_TH2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10235): error C4668: '_WIN32_WINNT_WIN10_RS1' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10240): error C4668: '_WIN32_WINNT_WIN10_TH2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10244): error C4668: '_WIN32_WINNT_WIN10_TH2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10251): error C4668: '_WIN32_WINNT_WIN10_RS2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10261): error C4668: '_WIN32_WINNT_WIN10_RS2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10265): error C4668: '_WIN32_WINNT_WIN10_TH2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10270): error C4668: '_WIN32_WINNT_WIN10_RS3' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10277): error C4668: '_WIN32_WINNT_WIN10_RS4' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10280): error C4668: '_WIN32_WINNT_WIN10_RS1' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10283): error C4668: '_WIN32_WINNT_WIN10_RS4' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10288): error C4668: '_WIN32_WINNT_WIN10_RS1' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10291): error C4668: '_WIN32_WINNT_WIN10_RS5' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10486): error C4668: '_WIN32_WINNT_WIN10_RS2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(10510): error C4668: '_WIN32_WINNT_WIN10_RS5' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(13950): error C4668: '_WIN32_WINNT_WIN10_RS5' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15111): error C4668: '_WIN32_WINNT_WIN10_RS3' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15128): error C4668: '_WIN32_WINNT_WIN10_RS3' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15147): error C4668: '_WIN32_WINNT_WIN10_RS2' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15266): error C4668: '_WIN32_WINNT_WIN10_RS3' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15416): error C4668: '_WIN32_WINNT_WIN10_RS4' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15429): error C4668: '_WIN32_WINNT_WIN10_RS4' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\winioctl.h(15657): error C4668: '_WIN32_WINNT_WIN10_RS1' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'

Is it impossible to import winsock2.h into unreal engine 4?

You need a “header sandwich” (bolded below) that allows you to include the winsock stuff. Here’s what the .h file should look like:



**// UE4: allow Windows platform types to avoid naming collisions
//  this must be undone at the bottom of this file
#include "AllowWindowsPlatformTypes.h"
#include "prewindowsapi.h"**

// these are our includes, yours may be different
#include <exception>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <mutex>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <thread>

#include "INetwork.h"

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

//
//
// YOUR TCP HEADER CODE HERE
//
//

**// UE4: disallow windows platform types
//  this was enabled at the top of the file
#include "PostWindowsApi.h"
#include "HideWindowsPlatformTypes.h"**


Also, just noticed that most of those includes should be forward declarations and move the includes to the .cpp file. :rolleyes: