0

Trying to execute this Scoket program in VSCode for Windows. But getting this error. Please provide some solutions thanks

#include<stdio.h>
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char *message;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
    
    printf("Initialised.\n");
    
    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");
    
    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
    
    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    
    puts("Bind done");

    //Listen to incoming connections
    listen(s , 3);
    
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    
    c = sizeof(struct sockaddr_in);
    
    while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
    {
        puts("Connection accepted");
        
        //Reply to the client
        message = "Hello Client , I have received your connection. But I have to go now, bye\n";
        send(new_socket , message , strlen(message) , 0);
    }
    
    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
        return 1;
    }

    closesocket(s);
    WSACleanup();
    
    return 0;
}
[Running] cd "c:\Users\test\Desktop\ClientServerSocket\Socket\" && g++ Client_Server.cpp -o Client_Server && "c:\Users\test\Desktop\ClientServerSocket\Socket\"Client_Server
Client_Server.cpp: In function 'int main(int, char**)':
Client_Server.cpp:62:13: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   message = "Hello Client , I have received your connection. But I have to go now, bye\n";
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x37): undefined reference to `WSAStartup@8'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x48): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x8a): undefined reference to `socket@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0xa0): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0xdb): undefined reference to `htons@4'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x102): undefined reference to `bind@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x114): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x14f): undefined reference to `listen@8'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x187): undefined reference to `accept@12'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x1d4): undefined reference to `send@16'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x1e4): undefined reference to `WSAGetLastError@0'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x206): undefined reference to `closesocket@4'
C:\Users\test\AppData\Local\Temp\ccEU59Du.o:Client_Server.cpp:(.text+0x20e): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status

273K
  • 29,503
  • 10
  • 41
  • 64
  • 3
    I doubt `#pragma comment(lib` is supported in gcc. This can help you https://stackoverflow.com/questions/47410667/link-to-external-library-with-g – 273K Aug 01 '21 at 08:20
  • compile your code using MVSC compiler (cl.exe) in Vscode configure default build task to c/c++ cl.exe – Adib Aug 01 '21 at 08:28
  • That's a well-known error class. Even your specific error cases are regularly asked about here. – Ulrich Eckhardt Aug 01 '21 at 08:36
  • If you want to build for `mingw` instead of `msvc` you will have to edit your `tasks.json` and add the command line setting to link to `ws2_32.lib` assuming you are not using an extension that changes the default build method. – drescherjm Aug 01 '21 at 16:02

0 Answers0