-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpServer.cpp
65 lines (51 loc) · 1.67 KB
/
httpServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
// mingw32-make.exe to make --> Not Working
// g++ .\httpServer.cpp -o server.exe -lws2_32 --> Working
int main()
{
// Initialize the winsocket
WSADATA wsaDATA;
WORD wVersionRequested = MAKEWORD(2, 2);
int result = WSAStartup(wVersionRequested, &wsaDATA);
if (result != 0)
{
printf("Error while initializing winsocket: %d", result);
return 1;
}
printf("Winsocket has been initialized \n");
// Creating a socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
printf("Error while creating the socket: " + serverSocket);
return 2;
}
// TODO: CREATE AN OUTPUT FOR THE SOCKET VALUE
printf("Socket has been created STATUS: %d\n", serverSocket);
// Bind the scoket
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(3000);
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.0");
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("Error while binding the socket: %d", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 3;
}
// Listen for connections
if (listen(serverSocket, 10) == SOCKET_ERROR) {
printf("Error while listening for connections: %d", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 4;
}
// Cleanup the winsocket
closesocket(serverSocket);
WSACleanup();
return 0;
}