Skip to content

Instantly share code, notes, and snippets.

@quantumcore
Created November 18, 2018 15:47
Show Gist options
  • Save quantumcore/9ccd4212c3d2bbae0de6ca607a85797a to your computer and use it in GitHub Desktop.
Save quantumcore/9ccd4212c3d2bbae0de6ca607a85797a to your computer and use it in GitHub Desktop.
Winsock Basic HTTP Server C++
/*
HTTP Server from Winsock made from scratch.
Author : Lynx [ Fahad Mustafa ]
Date 18 November 2018
*/
// Nessecary Includes.
#include "pch.h"
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <tchar.h>
// Buffer
#define Buffer 1500
// A Pragma comment. Loading Library
#pragma comment(lib, "ws2_32.lib")
int main()
{
// Variables and Declarations
WSADATA wsa;
struct sockaddr_in server;
SOCKET s;
SOCKET clientS;
int iResult;
const char message[Buffer] = "This message being displayed to you in your Browser.\nIs sent from a C++ Winsock HTTP Server.\n Now Closing Connection.\r";
char reply[Buffer] = { 0 };
// Simple. Start WSA(Windows Sockets API). If the return answer is not 0. It means error so therefore,
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
std::cerr << "WinsocK Failed" << std::endl; // Print out this on the Screen.
getchar(); // Pause Console
exit(true); // Exit Program, Note that in Computing, 1 = True, 0 = False. So 0 or 1 can also be entered as, exit(1);
}
std::cout << "Windows Socket API Started" << std::endl; // Notifiy user that, WSA (Windows Socket API) has started.
// Create a Network Socket
s = socket(AF_INET, SOCK_STREAM, NULL);
// If the Socket is Invalid or a Socket Error Occurs
if (s == SOCKET_ERROR)
{
std::cerr << "Socket Error : " << WSAGetLastError() << std::endl;
getchar(); // Pause Console
exit(true); // Exit Program, Note that in Computing, 1 = True, 0 = False. So 0 or 1 can also be entered as, exit(1);
}
else if (s == INVALID_SOCKET) {
std::cerr << "Socket Error : " << WSAGetLastError() << std::endl;
getchar(); // Pause Console
exit(true); // Exit Program, Note that in Computing, 1 = True, 0 = False. So 0 or 1 can also be entered as, exit(1);
}
std::cout << "Socket Created" << std::endl;
server.sin_family = AF_INET; // Using AF_INET Address Family.
server.sin_port = htons(80); // Defining PORT
InetPton(AF_INET, _T("0.0.0.0"), &server.sin_addr); // Defining The Network Address to Run the Server on
iResult = bind(s, (struct sockaddr*)&server, sizeof(server)); // binding the Host Address and Port Number
if (iResult == SOCKET_ERROR) // If Bind gives Error
{
std::cerr << "Bind Error " << WSAGetLastError() << std::endl;
getchar(); // Pause Console
exit(true); // Exit Program, Note that in Computing, 1 = True, 0 = False. So 0 or 1 can also be entered as, exit(1);
}
std::cout << "Listening on : 0.0.0.0:80" << std::endl; // Tell the User we Started Listening.
iResult = listen(s, AF_INET); // Then Actually Start Listening for incoming Connections.
/*
The Program will start to listen for incoming connections and will do so until
Someone Connects.
*/
clientS = accept(s, NULL, NULL); // Accept a Connection on a new Socket made for the Client.
if (clientS == SOCKET_ERROR) { // if Accepting Connection is a Error
std::cerr << "Accept FAiled!" << WSAGetLastError() << std::endl;
getchar(); // Pause Console
exit(true); // Exit Program, Note that in Computing, 1 = True, 0 = False. So 0 or 1 can also be entered as, exit(1);
}
else {
std::cout << "A Client Connected. Sending a Message and closing Connection" << std::endl; // notify user that someone connected
send(clientS, message, strlen(message), NULL); // Send Client a Message
}
recv(clientS, reply, sizeof(reply), NULL); // Just in case if the Client sends something, We Receive it.
closesocket(clientS); // close the Client Socket now that our Work is Complete.
WSACleanup(); // Clean Windows Socket API.
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment