Click here to Skip to main content
15,845,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

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;
//#define WIN32_LEAN_AND_MEAN

#include <iostream>
#include <string>
#include <memory>
#include <WinSock2.h>

const char* SOCKET_IP = "";
const int SOCKET_PORT = 1010;
const int BUFFER_SIZE = 1024;

int main(int argc, char** argv)
	WSAData wsa;
	WSAStartup(MAKEWORD(2, 2), &wsa);
	DWORD dwKeepAlive = 1;
	setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, (const char *) &dwKeepAlive, sizeof(dwKeepAlive));

	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));
	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);
		//recvfrom(server, buffer, BUFFER_SIZE, 0, (sockaddr *)&addr, &addr_size);
		printf("Response from %s: '%s'\n", inet_ntoa(addr.sin_addr), buffer);

		send(server, request, lstrlen(request), MSG_DONTROUTE);
		shutdown(server, SB_BOTH);


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.
Gerry Schmitz 30-Apr-21 19:44pm    
Connect the laptop to the internet, publish the IP address and a port, remove any firewalls, and you have a server.
deXo-fan 30-Apr-21 20:17pm    
How do I publish my IP address? Is it going to be a problem that, currently, I access the web through a hotspot? (meaning my IP varies)

And I don't want my laptop to be the server in the sense that people can access my files. I want to code my own server, sort of like the Apache server which many use to host PHP scripts. I want my program to sense when a person enters my IP and port as the url in their browser, and I want my program to be the one to send the html back to that person.
Dave Kreskowiak 1-May-21 0:03am    
You don't publish your IP address. You make your machine publicly visible to the internet. If you're behind a typical home router, you have to configure your router put your machine into what's called the DMZ. Consult the manual on your router to find out how to configure it.
Greg Utas 1-May-21 10:21am    
You have the minimal beginnings of a server, but what you plan on having it do? Be advised that a server should usually have at least two threads. The first receives TCP and puts client requests on a work queue. The second takes messages off this queue, processes them, and sends responses to the clients.

If you want to see a pile of code that does that kind of thing, I have a GitHub repository you can look at. I've written quite a few articles about it, but I haven't yet written anything about this aspect of server design. As far as the TCP part goes, the code for that thread is at
Jerry Jeremiah 4-May-21 2:22am    
When the program is running, you need a way for people to find out the ip address of your modem (look at DynDNS or other dynamic DNS services) and if the modem is separate from your computer you need to set up a port forwarding rule in the mode (look at the modem's owners manual). "http" is port 80. Then when somone connects to the name you have stored in the dynamic DNS it will translate to the ip address of your modem and the modem will forward the data to and from your computer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900