Hello,
I have a code for a server that works just fine on the localhost. I was wondering if I could use that same code to support and publish a website. Make it available online for others, that is. Put... on... internet... I don't think I can make it any simpler than that.
If the answer is yes, then please tell me what I would need in terms of equipment etc. Right now I only have my laptop which runs Windows 10 home.
If the answer is no, then please tell me why.
It might be useful to you to see me code, so I'll include it here.
Thank you!
#pragma comment(lib, "Ws2_32.lib")
typedef struct IUnknown IUnknown;
#include <iostream>
#include <string>
#include <memory>
#include <WinSock2.h>
const char* SOCKET_IP = "127.0.0.1";
const int SOCKET_PORT = 1010;
const int BUFFER_SIZE = 1024;
int main(int argc, char** argv)
{
WSAData wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
DWORD dwKeepAlive = 1;
setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, (const char *) &dwKeepAlive, sizeof(dwKeepAlive));
SOCKADDR_IN addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(SOCKET_PORT);
addr.sin_addr.S_un.S_addr = inet_addr(SOCKET_IP);
int addr_size = sizeof(addr);
bind(client, (const sockaddr *)&addr, addr_size);
listen(client, 8);
const char* request = "HTTP/1.1 200 OK\n\rConnection: Keep-Alive\n\r\n\rHi! Some HTML!\n\r";
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
printf("Listening...\n");
while (true)
{
SOCKET server = accept(client, (sockaddr *)&addr, &addr_size);
getsockname(server, (sockaddr *)&addr, &addr_size);
printf("Incoming connection from %s\n", inet_ntoa(addr.sin_addr));
recv(server, buffer, BUFFER_SIZE, 0);
printf("Response from %s: '%s'\n", inet_ntoa(addr.sin_addr), buffer);
send(server, request, lstrlen(request), MSG_DONTROUTE);
shutdown(server, SB_BOTH);
closesocket(server);
}
}
What I have tried:
What have I tried? Well, not much, but I'm about to try and get some help from some great guys.