1
https://cdn.discordapp.com/attachments/503211497766256642/969144581394407484/unknown.png

I haven't been able to fix this error for 3 days now, I'm working in VSCode, and all the solutions to a similar problem I found were for Clion or CodeBloks, someone knows a solution for VSCode? As far as I understood the problem is that I did not add any libraries. Or in this line #pragma comment(lib, “Ws2_32.lib”)

#include <stdio.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    
    #pragma comment(lib, “Ws2_32.lib”)
    
    int main(){
    
    
    WSADATA wsaData;    
    int iResult;
    
    
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);     
    
    if (iResult != 0)     
    {
         printf("WSAStartup failed: %d\n", iResult);
         return 1;
    }
    
    struct addrinfo *result = NULL, *ptr = NULL;     
    struct addrinfo hints;
    
    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP; 
    iResult = getaddrinfo("147.175.115.34", "777", &hints, &result);
    if (iResult != 0)     
    {
         printf("getaddrinfo failed: %d\n", iResult);
         WSACleanup();
         return 1;
    }
    else 
    printf("getaddrinfo fail…\n");
    
    SOCKET ConnectSocket = INVALID_SOCKET;
    ptr = result;
    
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,ptr->ai_protocol);
    
    if (ConnectSocket == INVALID_SOCKET)      
    {
         printf("Error at socket(): %ld\n", WSAGetLastError());
         freeaddrinfo(result);
         WSACleanup();
         return 1;
    }
    else
    printf("Error at socke occur…\n");
    
    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    
    if (iResult == SOCKET_ERROR)     
    printf("Not connected to server…\n");
    else
    printf("Connected to server!\n");
    
    if (iResult == SOCKET_ERROR)    
    {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        WSACleanup();
        return 1;
    }
    
    Sleep(250);
    
    char sendbuf[4096];
    
    iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
    if (iResult == SOCKET_ERROR)
    {
         printf("send failed: %d\n", WSAGetLastError());
         closesocket(ConnectSocket);
         WSACleanup();
         return 1;
    } 
    
    printf("Bytes Sent: %ld\n", iResult);
    
    #define DEFAULT_BUFLEN 4096 
    
    int recvbuflen = DEFAULT_BUFLEN;
    char recvbuf[DEFAULT_BUFLEN];
    
    
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);     
    if ( iResult > 0 )
    printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
    printf("Connection closed\n");     
    else
    printf("recv failed with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    }
Denis
  • 19
  • 2
  • 1
    The error reveals a linking error. It has generally nothing to do with the IDE, with the little exception that you set up the linking parameters in your project's setting. -- I suggest to first learn how to call compiler and linker correctly, and to understand that, before letting an IDE take the boring stuff. It definitely helps analyzing such problems. – the busybee Apr 28 '22 at 08:25
  • I'm pretty sure your compiler doesn't support `#pragma comment(lib, "Ws2_32.lib")`? This is specific to Microsoft compilers. With Microsoft compilers this pragma tell the compiler that the Ws2_32.lib library should be linked, but on your compiler this pragma is probably just ignored. Please [edit] and show the __verbatim__ error log in the question as properly formatted text. – Jabberwocky Apr 28 '22 at 08:27
  • Maybe this helps: https://stackoverflow.com/questions/2033608/mingw-linker-error-winsock – Jabberwocky Apr 28 '22 at 08:55

0 Answers0