#include <sys/types.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#define WEB_PORT 80
#define BROWSER_PORT 8085
bool is_message(char mess[4096])
{
if (mess[55] == 'G')
return true;
return false;
}
int main()
{
// initilize
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int startup = WSAStartup(ver, &wsData);
if (startup != 0)
{
printf("can't initialize!");
return 1;
}
// create socket object
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET)
{
printf("can't make server socket!");
return 1;
}
// define the address
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(BROWSER_PORT);
server_address.sin_addr.s_addr = INADDR_ANY;
// bind the socket to the correct address
bind(server_socket, (sockaddr*) &server_address, sizeof(server_address));
// listen to connect request
std::cout << "Listening to connection request" << std::endl;
listen(server_socket, SOMAXCONN);
sockaddr_in web_client;
int websize = sizeof(web_client);
char browser_message[4096];
while (1)
{
SOCKET web_socket = accept(server_socket,
(sockaddr*) &web_client,
&websize); // accept connection request
if (web_socket == INVALID_SOCKET)
{
printf("can't make web socket!");
return 1;
}
// receive message
recv(server_socket, browser_message, sizeof(browser_message), 0);
// show message
std::cout << "Message: " << std::endl;
for (size_t i = 0; i < sizeof(browser_message); i++)
std::cout << browser_message[i];
std::cout << std::endl;
// filter to see if the message is the GET request
if (is_message(browser_message))
{
continue;
}
std::cout << "Trash" << std::endl;
}
std::cout << "Message received!" << std::endl;
/* ---------------- Client part ------------------------------------*/
// // client part, sends GET message to the internet server
// char response[4096];
// int client_socket;
// client_socket = socket(AF_INET, SOCK_STREAM, 0);
// // // define the address
// char *address;
// struct sockaddr_in client_address;
// client_address.sin_family = AF_INET;
// client_address.sin_port = htons(WEB_PORT);
// inet_aton(address, &client_address.sin_addr.s_addr);
// // connect to the internet server
// connect(client_socket, (struct sockaddr *)&client_address, sizeof(client_address));
// send(client_socket, browser_message, sizeof(browser_message), 0);
// recv(client_socket, &response, sizeof(response), 0);
// close(client_socket);
// // show response
// std::cout << "Response: " << std::endl;
// for (size_t i = 0; i < sizeof(response); i++)
// std::cout << response[i];
// std::cout << std::endl;
std::cout << "done" << std::endl;
return 0;
}
I can't get this to compile with VScode. Can someone explain why? This is what i get in response.
Executing task: C/C++: g++.exe build active file <
Starting build...
C:\msys64\mingw64\bin\g++.exe -g "C:\Users\Martin F Lundgren\tdts06_lab\Lab2\proxy.cpp" -o "C:\Users\Martin F Lundgren\tdts06_lab\Lab2\proxy.exe"
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccxdMQUb.o:C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:29: undefined reference to `__imp_WSAStartup'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccxdMQUb.o: in function `main':
C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:37: undefined reference to `__imp_socket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:47: undefined reference to `__imp_htons'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:51: undefined reference to `__imp_bind'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:55: undefined reference to `__imp_listen'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:63: undefined reference to `__imp_accept'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Martin F Lundgren/tdts06_lab/Lab2/proxy.cpp:70: undefined reference to `__imp_recv'
collect2.exe: error: ld returned 1 exit status
Edit: when I try
g++ -std=c++17 -Wall -Wextra -lws2_32 proxy.cpp
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-
mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x5c): undefined reference to __imp_WSAStartup' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x9a): undefined reference to
__imp_socket'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0xd8): undefined reference to __imp_htons' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x109): undefined reference to
__imp_bind'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x143): undefined reference to __imp_listen' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x171): undefined reference to
__imp_accept'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MARTIN~1\AppData\Local\Temp\ccl2nHQn.o:proxy.cpp:(.text+0x1bb): undefined reference to `__imp_recv'