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