Created
April 25, 2016 17:35
-
-
Save astrcomp/4d13ae1c70ebbaa72130390227977279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <WinSock.h> | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
#pragma comment (lib,"ws2_32.lib") | |
SOCKADDR_IN addr; | |
int addrlen = sizeof(addr); | |
int Counter; | |
SOCKET sConnect; | |
SOCKET sListen; | |
SOCKET *Connections; | |
int InitWinSock() | |
{ | |
int Val = 0; | |
WSADATA wsaData; | |
WORD DllVersion = MAKEWORD(2,1); | |
Val = WSAStartup(DllVersion, &wsaData); | |
return Val; | |
} | |
int main() | |
{ | |
system("color 0a"); | |
cout << "Server started." << endl; | |
int Val = InitWinSock(); | |
if (Val != 0) | |
{ | |
MessageBoxA(NULL, "Error dont init windsocks", "Errors", MB_OK | MB_ICONERROR); | |
exit(1); | |
} | |
Connections = (SOCKET*)calloc(64, sizeof(SOCKET)); | |
sListen = socket(AF_INET, SOCK_STREAM, NULL); | |
sConnect = socket(AF_INET, SOCK_STREAM, NULL); | |
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
addr.sin_port = htons(80); | |
addr.sin_family = AF_INET; | |
bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); | |
listen(sListen, 64); | |
while (true) | |
{ | |
if (sConnect = accept(sListen, (SOCKADDR*)&addr,&addrlen)) | |
{ | |
Connections[Counter] = sConnect; | |
char *Name = new char[200]; | |
ZeroMemory(Name, 200); | |
int size = sizeof("<html>\r\n<head><title>Яндекс</title></head>\r\n<body>\r\ntest %i\r\n</body>\r\n</html>"); | |
int first = sizeof("HTTP/1.1 200 OK\r\nContentType = text/html; charset=utf-8\r\nContent-length: %i\r\nConnection: close\r\n\r"); | |
sprintf_s(Name, 200, "HTTP/1.1 200 OK\r\nContentType = text/html; charset=utf-8\r\nContent-length: %i\r\nConnection: close\r\n\r\n", size);// <html>\r\n<head><title>Яндекс< / title>< / head>\r\n<body>\r\ntest %i\r\n< / body>\r\n< / html>\r\n\r\n", size,Counter); | |
int index = 0; | |
int sizeResponse = 0; | |
while (true) { | |
if (Name[index] == NULL) { | |
sizeResponse = index; | |
break; | |
} | |
index++; | |
} | |
send(Connections[Counter], Name, sizeResponse, NULL); | |
ZeroMemory(Name, 200); | |
sprintf_s(Name, 200, "<html>\r\n<head><title>Яндекс</title></head>\r\n<body>\r\ntest %i\r\n</body>\r\n</html>\r\n\r\n", sizeResponse); | |
send(Connections[Counter], Name, size, NULL); | |
cout << "New Connection" << endl; | |
Counter++; | |
} | |
Sleep(50); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment