Simple Windows Server

In an effort to post something completely different. I’m putting up a quick and dirty Windows-based server I recently created. You may or may not have issues compiling this on various versions of Windows. Keep in mind this server is far from robust. It has pretty much no features as of yet. This really was a lesson for me to learn how to set up sockets on a Windows box. What the code is supposed to do is listen on local port 80 for incoming connections. After that it does nothing. I think in future versions I will have it serve a page or perform some function; we’ll see.

#define WIN32_LEAN_AND_MEAN

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

struct SOCKADDR_IN
{
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};

int main()
{
WSAData wsa;
WORD Version = MAKEWORD(2, 1);

WSAStartup(Version, &wsa);

SOCKET Listen = socket(AF_INET,SOCK_STREAM,NULL);
SOCKET Connect = socket(AF_INET,SOCK_STREAM,NULL);

SOCKADDR_IN Server;

Server.sin_addr.s_addr = inet_addr("127.0.0.1");
Server.sin_family = AF_INET;
Server.sin_port = htons(80);

bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

listen(Listen, 1);

int size = sizeof(Server);

std::cout << "Listening for connection...";

for(;;)
{
if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
std::cout << "Connection found\n";
break;
}

WSACleanup();
std::cin.get();
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s