Created
April 29, 2016 10:29
-
-
Save astrcomp/db5e048db41689a02303181564fc34c3 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 <WinSock2.h> | |
#include <iostream> | |
#pragma comment (lib,"ws2_32.lib") | |
using namespace std; | |
WSADATA w; | |
DWORD dwTemp,FileSize = 0; | |
HANDLE io; | |
static char *bodyhtml; | |
SOCKET SockListen ; | |
sockaddr_in addr = { AF_INET ,htons(80) ,htonl(INADDR_ANY), }; | |
void ReadFileHTML(wchar_t str[]) | |
{ | |
string send; | |
DWORD dwCounter; | |
io = CreateFile(str, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
FileSize = GetFileSize(io, NULL); | |
bodyhtml = new char[FileSize +1]; | |
ReadFile(io, bodyhtml, FileSize, &dwTemp, NULL); | |
bodyhtml[FileSize] = 0; | |
CloseHandle(io); | |
cout << "!!!!!!!!!!!!!!!! " << bodyhtml[0] << endl; | |
} | |
void screen() | |
{ | |
system("cls"); | |
system("color 1B"); | |
io = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD cursorPos; | |
COORD OldCursorPos; | |
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
GetConsoleScreenBufferInfo(io, &csbiInfo); | |
OldCursorPos.X = csbiInfo.dwCursorPosition.X; | |
OldCursorPos.Y = csbiInfo.dwCursorPosition.Y+1; | |
cursorPos.X = csbiInfo.srWindow.Right - strnlen_s(" console top= ",200) - strnlen_s(" console shirina = ", 200)-sizeof(csbiInfo.srWindow.Bottom); | |
cursorPos.Y = 0; | |
SetConsoleCursorPosition(io, cursorPos); | |
cout << " console top= " << csbiInfo.srWindow.Bottom << " console shirina= " << csbiInfo.srWindow.Right << endl; | |
cursorPos.X = csbiInfo.srWindow.Right - strnlen_s("file size = ", 200)-3; | |
cursorPos.Y = 24; | |
SetConsoleCursorPosition(io, cursorPos); | |
cout << "file size = " << FileSize; | |
SetConsoleCursorPosition(io, OldCursorPos); | |
} | |
void WSAStart() | |
{ | |
cout << "Server started!" << endl; | |
WORD verWSAData = MAKEWORD(02, 02); | |
int error = WSAStartup(verWSAData, &w); | |
uint8_t minor = w.wVersion & 0xFF; | |
uint8_t major = w.wVersion >> 8; | |
error = (int)minor; | |
cout << "version = " << error; | |
error = (int)major; | |
cout << "." << error << endl; | |
SockListen = socket(AF_INET, SOCK_STREAM, 0); | |
bind(SockListen, (LPSOCKADDR)&addr, sizeof(addr)); | |
} | |
int countMass(char str[]) | |
{ | |
int wordsize = 0; | |
while (str[wordsize] != NULL) ++wordsize; | |
return wordsize; | |
} | |
void findandreplace(char str[],int num) | |
{ | |
char test[6]; | |
_itoa_s(num, test, 10); | |
int wordsize = countMass(str); | |
int countofinput = countMass(test); | |
int EndSize = FileSize - wordsize + countofinput; | |
char * vrem = new char[FileSize]; | |
strcpy_s(vrem,(FileSize+1), bodyhtml); | |
delete []bodyhtml; | |
bodyhtml = new char[EndSize]; | |
//cout << vrem << endl; | |
//cout << "test " << test << endl; | |
//cout << endl; | |
int b = 0; | |
int kol = 0; | |
for (int i = 0; i < FileSize+1; i++) | |
{ | |
if (vrem[i] == str[kol]) { | |
if (kol == wordsize-1 ) { | |
for (int z = 0; z <countofinput; ++z) | |
{ | |
bodyhtml[i - wordsize+1+z] = test[z]; | |
// cout << bodyhtml[i - wordsize+1 + z]; | |
} | |
b = countofinput - wordsize; | |
kol = 0; | |
} | |
else kol++; | |
} | |
else | |
{ | |
if (kol != 0) kol = 0; | |
bodyhtml[i+b] = vrem[i]; | |
// cout << bodyhtml[i]; | |
} | |
} | |
// printf(bodyhtml); | |
// cout << "itog " << countMass(bodyhtml) << " raz! " <<" razmer slova = "<< wordsize<< " slovo = "<< str<< endl; | |
} | |
int main() | |
{ | |
ReadFileHTML(L"HTTPhead"); | |
screen(); | |
findandreplace("%i", 110); | |
WSAStart(); | |
listen(SockListen, 30); | |
int numberofclients = 0; | |
SOCKET client[10]; | |
sockaddr client_sock[10]; | |
char reciver[400]; | |
while (numberofclients < 10) | |
{ | |
client[numberofclients+1] = accept(SockListen, (LPSOCKADDR)&client_sock[numberofclients], NULL); | |
if (client[numberofclients+1] == INVALID_SOCKET) | |
{ // ошибка соединения | |
WSACleanup(); | |
return 0; | |
} | |
else | |
{ // клиент успешно присоединился | |
// запускаем поток для связи с клиентом | |
cout << "Player joined! " << numberofclients + 1 << endl; | |
char buffer[2]; | |
recv(client[numberofclients + 1], reciver, 300, 0); | |
int stringlen = strlen(reciver); | |
cout <<stringlen<<" "<< reciver << endl; | |
ReadFileHTML(L"index.html"); | |
int size = FileSize; | |
cout << FileSize; | |
ReadFileHTML(L"HTTPhead"); | |
findandreplace("%i", size); | |
send(client[numberofclients + 1], bodyhtml, FileSize, NULL); | |
send(client[numberofclients + 1], "\r\n\r\n\r\n", 6, NULL); | |
printf(bodyhtml); | |
ReadFileHTML(L"index.html"); | |
send(client[numberofclients + 1], bodyhtml, FileSize, NULL); | |
cout << " answer file size "<< 110 << endl; | |
printf(bodyhtml); | |
numberofclients++; | |
} | |
} | |
recv(SockListen, reciver, 300, 0); | |
cout << reciver << endl; | |
system("pause"); | |
shutdown(SockListen, SD_SEND); | |
closesocket(SockListen); | |
WSACleanup(); // выгружаем ws2_32.dll | |
SetConsoleTextAttribute(io, BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY); | |
printf("Test string: red on blue\n"); | |
printf("dasdasdsdasdada "); | |
//printf("%d %d", csbiInfo.dwCursorPosition.X, csbiInfo.dwCursorPosition.Y); | |
// Повторяем с другими координатами... | |
//cursorPos.X = 15; | |
//cursorPos.Y = 8; | |
//SetConsoleCursorPosition(io, cursorPos); | |
cout << "t "; | |
printf("Test string at position (15, 8)"); | |
//delete[] virgRDWRBuff; | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment