-1

I am trying to write a simple socket code in codelite on windows but I can not import ws2_32.h so my project is not working and I am getting "No such file or directory".How can i fix this problem.I tried to write this in the linker options but I could not be successful.I researched the problems like this but I could not understand.This is a new version of Codelite and different from others.I am using MinGW as compiler.

Edit: I removed the library which one unnecessary and wrong(#include "ws2_32.h") and when I add "-Iws2_32" to the linker options problem solved.

#define _WINSOCK_DEPRECATED_NO_WARNINGS
//#ifndef WIN32_LEAN_AND_MEAN
//#define WIN32_LEAN_AND_MEAN
//#endif
#include <stdio.h>
#include <string.h>
#include "winsock2.h"
#include "ws2_32.h"
#include <WS2tcpip.h>



#define SERVER_BUF 10
#define SAME       0
#define G_BUF  1024


int main()
{
    WSADATA wsa=NULL;
    SOCKET s, new_socket;
    char buf[SERVER_BUF];
    char  gbuf[G_BUF];
    struct sockaddr_in server_information;
    struct sockaddr_in client_information;

    WSAStartup(MAKEWORD(2, 2), &wsa);
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        printf("Failed. Error Code : %d", WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");


    server_information.sin_family = AF_INET;
    server_information.sin_addr.s_addr = inet_addr("192.168.10.16");
    server_information.sin_port = htons(8888);



    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s != -1)
    {
        printf("Soket created...");
    }


    if (bind(s, (struct sockaddr*)&server_information, sizeof(server_information)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d", WSAGetLastError());
    }

    puts("Bind done");
    while (s != 0) {
        if (listen(s, 1) != -1)
        {
            printf("Listening is successful.");
        }

        socklen_t size = sizeof(client_information);
        new_socket = accept(s, (struct sockaddr*)&client_information, &size);


        if (new_socket == -1)
        {
            printf("Accept failed");
            return -2;
        }
        else
            puts("Connection accepted");

        for (;;) {
            int res = recv(new_socket, buf, SERVER_BUF, 0);
            printf("amount of bytes received:%d\n", res);
            puts(buf);


            fgets(gbuf, G_BUF, stdin);
            send(new_socket, gbuf,strlen(gbuf)+1, 0);

            if (strcmp(buf, "exit") == SAME)
                break;
        }
    }

    return 0;
}

n_stckwf
  • 25
  • 7
  • I tried something on code and i realized the problem is this but i could not solve. – n_stckwf Aug 17 '21 at 14:59
  • An include statement, "#include "ws2_32.h" is used by the compile (technically the preprocessor) to add the contents the the included file into your source code. The first thing you will need to do is determine where "ws2_32.h" is found on your system. Next, you will need to pass the path to the compile so it can find the file. On the command line it would look like 'gcc -I c:\user\include' - you must replace the path with the one on your system. I have never used codelite, so I do not know where you enter that information. – thurizas Aug 17 '21 at 15:13
  • The Windows SDK is compatible with Microsoft's compiler. If you use a different compiler you're on your own. Regardless, the point is, don't "bump " a question, by re-asking essentially the same all over again. This is not how Stack Overflow works. – IInspectable Aug 17 '21 at 15:13
  • The simple answer is delete the line trying to include it. Why do you believe it exists and what do you hope to include from it? There is a ws2_32.lib but as far as I know the headers for that library are WinSock2.h and WS2tcpip.h. – Retired Ninja Aug 17 '21 at 15:34
  • Okay thank you.I wrote this because as far as I researched this library is necessery for sockaddr and so forth codes.my compiler does not identify that. – n_stckwf Aug 18 '21 at 05:09
  • Is there a convincing reason, why you aren't using a supported compiler, i.e. [Visual Studio](https://visualstudio.microsoft.com/)? MinGW is just horribly outdated, and there's literally nothing to be had from using it. – IInspectable Aug 18 '21 at 10:30
  • @IInspectable g++ is up to date and in active development. mingw-w64 is GCC with a different C library than glibc. – M.M Aug 26 '21 at 09:40

1 Answers1

0

The ws2_32 library is indeed necessary for socket functionality on Windows.

So you must link with the -lws2_32 flag.

But the include line should be #include <winsock2.h>, and - depending on which functions you call - possibly others like #include <ws2tcpip.h>.

Note that as these libraries are considered system include files on Windows you should use angle brackets (<>) instead of double quotes ("").

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40