-1

I am new to Windows socket programming in C++, where I was following this tutorial and had errors.

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#define _CRT_SECURE_NO_WARNINGS

//https://guidedhacking.com/threads/c-winsock-networking-tutorial-introduction.12131/

#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#define PORT 80

const char szHost[] = "www.google.com";

int main(const int argc, const char *argv[]) {
    // Init WINSOCK
    WSAData wsaData;
    WORD DllVersion = MAKEWORD(2, 1);
    if (WSAStartup(DllVersion, &wsaData) != 0) {
        fprintf(stderr, "WSAStartup()\n");
        getchar();
        ExitProcess(EXIT_FAILURE);
    }

    // Create socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        fprintf(stderr, "socket()\n");
        getchar();
        ExitProcess(EXIT_FAILURE);
    }

    // Get Server info
    HOSTENT *host = gethostbyname(szHost);
    if(host == nullptr) {
        closesocket(sock);
        fprintf(stderr, "gethostbyname()\n");
        getchar();
        ExitProcess(EXIT_FAILURE);
    }

    // Define server info
    SOCKADDR_IN sin;
    ZeroMemory(&sin, sizeof(sin));
    sin.sin_port = htons(PORT);
    sin.sin_family = AF_INET;
    memcpy(&sin.sin_addr.S_un.S_addr, host->h_addr_list[0], sizeof(sin.sin_addr.S_un.S_addr));

    // Connect to server
    if (connect(sock, (const sockaddr *)&sin, sizeof(sin)) != 0) {
        closesocket(sock);
        fprintf(stderr, "connect()\n");
        getchar();
        ExitProcess(EXIT_FAILURE);
    }
    // Send data to server
    const char szMsg[] = "HEAD / HTTP/1.0\r\n\r\n";
    if (!send(sock, szMsg, strlen(szMsg), 0)) {
        closesocket(sock);
        fprintf(stderr, "send()\n");
        getchar();
        ExitProcess(EXIT_FAILURE);
    }

    // Recieve data back from server
    char szBuffer[4096];
    char szTemp[4096];
    while (recv(sock, szTemp, 4096, 0))
        strcat(szBuffer, szTemp);

    printf("%s\n", szBuffer);

    closesocket(sock);
    getchar();
    ExitProcess(EXIT_SUCCESS);
}
PS C:\Users\nahar\Desktop\AutomatePY\C Progam\cpp> cd "c:\Users\nahar\Desktop\AutomatePY\C Progam\cpp\" ; if ($?) { g++ NewFIle.cpp -o NewFIle } ; if ($?) { .\NewFIle }cd "c:\Users\nahar\Desktop\AutomatePY\C Progam\cpp\" ; if ($?) { g++ NewFIle.cpp -o NewFIle } ; if ($?) { .\NewFIle }
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x2c): undefined reference to `WSAStartup@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x47): undefined reference to `htons@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x77): undefined reference to `socket@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x9a): undefined reference to `bind@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0xc5): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0xcd): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0xe0): undefined reference to `listen@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x10b): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x113): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x12e): undefined reference to `accept@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x159): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x161): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x1a4): undefined reference to `recv@16'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x1cf): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x1d7): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x215): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccXo2HJT.o:NewFIle.cpp:(.text+0x21d): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x2c): undefined reference to `WSAStartup@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x47): undefined reference to `htons@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x77): undefined reference to `socket@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x9a): undefined reference to `bind@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0xc5): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0xcd): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0xe0): undefined reference to `listen@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x10b): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x113): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x12e): undefined reference to `accept@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x159): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x161): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x1a4)c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x1cf): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x1d7): undefined reference to `WSACleanup@0'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x215): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\ccIJa9ck.o:NewFIle.cpp:(.text+0x21d): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status
PS C:\Users\nahar\Desktop\AutomatePY\C Progam\cpp> cd "c:\Users\nahar\Desktop\AutomatePY\C Progam\cpp\" ; if ($?) { g++ NewFIle.cpp -o NewFIle } ; if ($?) { .\NewFIle }
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x37): undefined reference to `WSAStartup@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x98): undefined reference to `socket@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0xaa): undefined reference to `gethostbyname@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0xc1): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x127): undefined reference to `htons@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x167): undefined reference to `connect@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x17e): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x21b): undefined reference to `send@16'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x232): undefined reference to `closesocket@4'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x293): undefined reference to `recv@16'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nahar\AppData\Local\Temp\cc7NV8AB.o:NewFIle.cpp:(.text+0x2d2): undefined reference to `closesocket@4'
collect2.exe: error: ld returned 1 exit status
1
  • 1
    gcc does not interpret that pragma. You need to explicitly link with ws2_32 and winsock when compiling/linking.
    – Botje
    Oct 27, 2020 at 13:00

1 Answer 1

1

It looks like you are using MinGW for windows which leads me to believe that you are using CodeBlocks or Eclipse (or a similar IDE). The code you are using isn't wrong, its just that it needs to be linked correctly.

The line

#pragma comment(lib, "ws2_32.lib")

Is kinda specific to Visual Studio. If you are using CodeBlocks, you'll need to use the GUI to add the ws2_32 library in the linker directives. Eclipse (or the IDE that you are using) would require similar steps.

You might wanna read a walk-through for CodeBlocks and a previously answered question.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.