1. // BPPA_SERVER.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5. // xsiebe03 (FEKT)
  6. // xrusna02 (FIT)
  7.  
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <windows.h>
  12. #include <string>
  13. #include <winsock.h>
  14. #include <fstream>
  15.  
  16. #pragma comment (lib,"WS2_32.lib")
  17. #define BUFSIZE 1000
  18.  
  19. using namespace std;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. WORD wVersionRequested = MAKEWORD(1,1); // Číslo verze
  24. WSADATA data; // Struktura s info. o knihovně;
  25. std::string text; // Přijímaný text
  26. sockaddr_in sockName; // "Jméno" soketu a číslo portu
  27. sockaddr_in clientInfo; // Klient, který se připojil
  28. SOCKET mainSocket; // Soket
  29. int port; // Číslo portu
  30. char buf[BUFSIZE]; // Přijímací buffer
  31.  
  32. int size; // Počet přijatých a odeslaných bytů
  33. int addrlen; // Velikost adresy vzdáleného počítače
  34. int count = 0; // Počet připojení
  35.  
  36. while(1){
  37. if (WSAStartup(wVersionRequested, &data) != 0)
  38. {
  39. cout << "Nepodarilo se inicializovat sokety" << endl;
  40. return -1;
  41. }
  42. port = 80;
  43. if ((mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
  44. == INVALID_SOCKET)
  45. {
  46. cerr << "Nelze vytvorit soket" << endl;
  47. WSACleanup();
  48. return -1;
  49. }
  50. // Zaplníme strukturu sockaddr_in
  51. // 1) Rodina protokolů;
  52. sockName.sin_family = AF_INET;
  53. // 2) Číslo portu, na kterém čekáme
  54. sockName.sin_port = htons(port);
  55. // 3) Nastavení IP adresy lokální síťové karty, přes kterou
  56. // je možno se připojit.
  57. // Nastavíme možnost připojit se odkudkoliv.
  58. sockName.sin_addr.s_addr = INADDR_ANY;
  59. //přiřadíme soketu jméno
  60. if (bind(mainSocket, (sockaddr *)&sockName, sizeof(sockName))
  61. == SOCKET_ERROR)
  62. {
  63. cerr << "Problem s pojemnovanim soketu" << endl;
  64. WSACleanup();
  65. return -1;
  66. }
  67.  
  68. // Vytvoříme frontu požadavků na spojení.
  69. // Vytvoříme frontu maximální velikosti 10 požadavků.
  70. cout << "Nasloucham na portu:" << port << endl;
  71. if (listen(mainSocket, 10) == SOCKET_ERROR)
  72. {
  73. cerr << "Problem s vytvorenim fronty" << endl;
  74. WSACleanup();
  75. return -1;
  76. }
  77.  
  78. // Poznačím si velikost struktury clientInfo.
  79. // Předám to funkci accept.
  80. addrlen = sizeof(clientInfo);
  81. // Vyberu z fronty požadavek na spojení.
  82. // "client" je nový soket spojující klienta se serverem.
  83. SOCKET client = accept(mainSocket, (sockaddr*)&clientInfo,
  84. &addrlen);
  85. int totalSize = 0;
  86. if (client == INVALID_SOCKET)
  87. {
  88. cerr << "Problem s prijetim dat" <<endl;
  89. WSACleanup();
  90. return -1;
  91. }
  92. // Zjistím IP adresu klienta.
  93. cout << "Nekdo se pripojil z adresy: "
  94. << inet_ntoa((in_addr)clientInfo.sin_addr) << endl;
  95. // Přijmu data. Ke komunikaci s klientem používám soket
  96. // "client"
  97.  
  98.  
  99.  
  100. text = "";
  101. // Prijmuti dat od clienta
  102. for (int i=0;i<BUFSIZE;i++)
  103. buf[i] = 0;
  104. while (totalSize <= 50)
  105. {
  106. if ((size = recv(client, buf, BUFSIZE-1, 0))
  107. == SOCKET_ERROR)
  108. {
  109. cerr << "Problem s prijetim dat." << endl;
  110. WSACleanup();
  111. return -1;
  112. }
  113. cout << "Prijato:" << size << endl;
  114. totalSize += size;
  115. text.append(buf);
  116.  
  117. }
  118.  
  119. //segmentace textu ze zpravy klienta
  120. int zero=text.find('\n');
  121. text.erase(zero);
  122. string cesta;
  123. int zero3 = text.rfind(' ');
  124. text.erase(zero3);
  125. zero = text.find('/');
  126. cesta.append(text,zero+1,(text.length())-(zero+1));
  127.  
  128. cout << endl << "Obsah pole je:" << cesta << endl;
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. //Prace se souborem
  136. ifstream myfile;
  137. string line,obsah;
  138. myfile.open (cesta.c_str());
  139. if (myfile.is_open())
  140. {
  141. while (! myfile.eof() )
  142. {
  143. getline (myfile,line);
  144. obsah +=line;
  145. obsah +='\n';
  146. }
  147. cout << "Obsah souboru:" << endl << obsah;
  148. myfile.close();
  149. }
  150. else
  151. {
  152. myfile.close();
  153. myfile.clear();
  154. myfile.open ("404.html");
  155. if (myfile.is_open())
  156. {
  157. while (! myfile.eof() )
  158. {
  159. getline (myfile,line);
  160. obsah +=line;
  161. obsah +='\n';
  162. }
  163. cout << "Obsah souboru:" << endl << obsah;
  164. myfile.close();
  165. }
  166. }
  167.  
  168.  
  169.  
  170.  
  171. string OK = "HTTP/1.1 200 OK\r\nAccept-ranges: bytes\r\nContent-length: 82\r\nContent-type: text/html\r\n";
  172. // Odeslu potvrzeni komunikace
  173. if ((size = send(client, OK.c_str(), OK.length(), 0))==SOCKET_ERROR)
  174. {
  175. cerr << "Problem s odeslanim dat" << endl;
  176. WSACleanup();
  177. return -1;
  178. }
  179. if ((size = send(client, obsah.c_str(), obsah.length(), 0))==SOCKET_ERROR)
  180. {
  181. cerr << "Problem s odeslanim dat" << endl;
  182. WSACleanup();
  183. return -1;
  184. }
  185. cout << "Odeslano: " << size << endl;
  186. // Uzavřu spojení s klientem
  187. closesocket(client);
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. cout << "Koncim" << endl << endl << endl;
  195. closesocket(mainSocket);
  196. WSACleanup();
  197. }
  198.  
  199. return 0;
  200. }
  201.