Advertisement
Guest User

Untitled

a guest
May 29th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock2.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. #include <time.h>
  7.  
  8. #define SERVER "SK_ProjektSerwer/1.0"
  9. #define PROTOCOL "HTTP/1.1"
  10. #define RFC1123FMT "%a, %d %b %Y %H:%M:%S"
  11. #define PORT 80
  12.  
  13.  
  14. int main()
  15. {
  16. FILE *plik, *plikbuf, *godz;
  17. WSADATA wsaData;
  18. WORD wVersionRequested = MAKEWORD(2, 2);
  19. SOCKET sock;
  20. SOCKET AcceptSock;
  21. fd_set fds;
  22. char timebuf[128];
  23. time_t now;
  24. char bufor[4096];
  25. char *sciezka, *metoda, *protokol;
  26. char sendBufor[50000], sendHeader[50000], sendHTML[50000];
  27. struct sockaddr_in adres;
  28. char *adresS;
  29. struct hostent *remoteHost;
  30. int length=0, i=0;
  31.  
  32. while(1)
  33. {
  34. // WSASTARTUP
  35. int err = WSAStartup(wVersionRequested, &wsaData);
  36. if (err != 0)
  37. {
  38. printf("WSAStartup blad numer: %d\n", err);
  39. return 1;
  40. }
  41.  
  42.  
  43. sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  44. if (sock == INVALID_SOCKET)
  45. printf("Blad funkcji socket = %d\n", WSAGetLastError() );
  46. else
  47. {
  48. printf("Funkcja socket wykonana poprawnie\n");
  49. }
  50.  
  51.  
  52.  
  53. adres.sin_addr.s_addr=inet_addr("127.0.0.1");
  54. adres.sin_family=AF_INET;
  55. adres.sin_port=htons(PORT);
  56.  
  57.  
  58.  
  59. int iResult = bind(sock, (SOCKADDR *) &adres, sizeof (adres));
  60. if (iResult == SOCKET_ERROR)
  61. {
  62. printf("Blad funkcji bind nr %u\n", WSAGetLastError());
  63. closesocket(sock);
  64. WSACleanup();
  65. return 1;
  66. }
  67. else
  68. printf("Poprawnie wykonano funkcje bind\n");
  69.  
  70.  
  71.  
  72. if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
  73. printf("listen function failed with error: %d\n", WSAGetLastError());
  74.  
  75. printf("Listening on socket...\n");
  76.  
  77. AcceptSock = accept(sock, NULL, NULL);
  78. if (AcceptSock == INVALID_SOCKET)
  79. {
  80. printf("accept failed with error: %d\n", WSAGetLastError());
  81. closesocket(sock);
  82. WSACleanup();
  83. return 1;
  84. }
  85. else
  86. printf("Client connected.\n\n");
  87.  
  88. memset(bufor,0,sizeof(bufor));
  89.  
  90. iResult = recv(AcceptSock,bufor, sizeof(bufor), 0);
  91. if (iResult == SOCKET_ERROR)
  92. {
  93. printf("Blad funkcji recvfrom nr: %d\n", WSAGetLastError());
  94. }
  95.  
  96. printf("%s\n",bufor);
  97.  
  98. metoda = strtok(bufor,"/");
  99. printf("%s\n",metoda);
  100.  
  101. sciezka = strtok(NULL," ");
  102. printf("%s\n",sciezka);
  103.  
  104. protokol = strtok(NULL,"\r");
  105. printf("%s\n",protokol);
  106.  
  107.  
  108. if(!strcmp(protokol,PROTOCOL))
  109. {
  110. // ******************* METODA GET ********************
  111. if (!strncmp(metoda, "GET", 3))
  112. {
  113. if(!strcmp(sciezka,"godzina.html"))
  114. {
  115. //*********** Dynamiczne generowanie strony z godzina ****************
  116. godz = fopen("godzina.html","w");
  117. now = time(NULL);
  118. strftime(timebuf, sizeof(timebuf), "%H:%M:%S", localtime(&now));
  119. fprintf(godz, "<html>\n<head>\n</head>\n<body>\n<center><i><b>Jest godzina: %s</b></i></center>\n</body>\n</html>", timebuf);
  120. fclose(godz);
  121. }
  122.  
  123. plik = fopen(sciezka,"rt");
  124. if(plik!=NULL)
  125. {
  126.  
  127. plikbuf = fopen("dane.txt","wt");
  128.  
  129. memset(sendHeader,0,sizeof(sendHeader));
  130. memset(sendHTML,0,sizeof(sendHTML));
  131. memset(sendBufor,0,sizeof(sendBufor));
  132.  
  133. fprintf(plikbuf, "%s 200 OK\r\n", PROTOCOL);
  134. now = time(NULL);
  135. strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
  136. fprintf(plikbuf, "Date: %s\r\n", timebuf);
  137. fprintf(plikbuf, "Server: %s\r\n", SERVER);
  138. fprintf(plikbuf, "Content-Type: text/html \r\n");
  139.  
  140. length = fread(sendHTML,1, sizeof(sendHTML), plik);
  141. fprintf(plikbuf, "Content-Length: %d \r\n", length);
  142. fprintf(plikbuf, "Connection: close \r\n");
  143. fprintf(plikbuf, "\r\n");
  144. fclose(plikbuf);
  145. plikbuf = fopen("dane.txt","rt");
  146. fread(sendHeader,1, sizeof(sendHeader), plikbuf);
  147. strcat(sendHeader,sendHTML);
  148. strcpy(sendBufor,sendHeader);
  149. fclose(plikbuf);
  150. fclose(plik);
  151. }
  152. else
  153. {
  154. //************************ Jesli nie znaleziono pliku ******************
  155. plikbuf = fopen("dane.txt","wt");
  156.  
  157. memset(sendHeader,0,sizeof(sendHeader));
  158. memset(sendBufor,0,sizeof(sendBufor));
  159.  
  160. fprintf(plikbuf, "%s 404 Not Found\r\n",PROTOCOL);
  161. now = time(NULL);
  162. strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
  163. fprintf(plikbuf, "Date: %s\r\n", timebuf);
  164. fprintf(plikbuf, "Server: %s\r\n", SERVER);
  165. fprintf(plikbuf, "Connection: close \r\n");
  166. fprintf(plikbuf, "\r\n");
  167. fclose(plikbuf);
  168. plikbuf = fopen("dane.txt","rt");
  169. fread(sendHeader,1, sizeof(sendHeader), plikbuf);
  170. strcpy(sendBufor,sendHeader);
  171. fclose(plikbuf);
  172. }
  173. }
  174. //**********************METODA HEAD******************
  175. else if(!strncmp(metoda, "HEAD", 4))
  176. {
  177.  
  178. memset(sendBufor,0,sizeof(sendBufor));
  179. strcpy(sendBufor,"Zapytanie HEAD jeszcze nie oprogramowane") ;
  180.  
  181. }
  182. else
  183. {
  184. //*********************METODA NIEPRAWIDLOWA***********
  185. plikbuf = fopen("dane.txt","wt");
  186.  
  187. memset(sendHeader,0,sizeof(sendHeader));
  188. memset(sendBufor,0,sizeof(sendBufor));
  189.  
  190. fprintf(plikbuf, "HTTP Error 405 Method not allowed\r\n");
  191. now = time(NULL);
  192. strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
  193. fprintf(plikbuf, "Date: %s\r\n", timebuf);
  194. fprintf(plikbuf, "Server: %s\r\n", SERVER);
  195. fprintf(plikbuf, "Connection: close \r\n");
  196. fprintf(plikbuf, "\r\n");
  197. fclose(plikbuf);
  198. plikbuf = fopen("dane.txt","rt");
  199. fread(sendHeader,1, sizeof(sendHeader), plikbuf);
  200. strcpy(sendBufor,sendHeader);
  201. fclose(plikbuf);
  202.  
  203. }
  204. }
  205. else
  206. {
  207. //******************** ZĀ£Y PROTOKOL **********************
  208. plikbuf = fopen("dane.txt","wt");
  209.  
  210. memset(sendHeader,0,sizeof(sendHeader));
  211. memset(sendBufor,0,sizeof(sendBufor));
  212.  
  213. fprintf(plikbuf, "HTTP 505 Version Not Supported\r\n");
  214. now = time(NULL);
  215. strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
  216. fprintf(plikbuf, "Date: %s\r\n", timebuf);
  217. fprintf(plikbuf, "Server: %s\r\n", SERVER);
  218. fprintf(plikbuf, "Connection: close \r\n");
  219. fprintf(plikbuf, "\r\n");
  220. fclose(plikbuf);
  221. plikbuf = fopen("dane.txt","rt");
  222. fread(sendHeader,1, sizeof(sendHeader), plikbuf);
  223. strcpy(sendBufor,sendHeader);
  224. fclose(plikbuf);
  225.  
  226. }
  227. printf("\n**************Wyslalem******************\n\n%s\n",sendBufor);
  228. printf("\n***************************************\n");
  229.  
  230. if( send(AcceptSock , sendBufor , strlen(sendHeader)+length , 0) < 0)
  231. {
  232. puts("Send failed");
  233. return 1;
  234. }
  235.  
  236. puts("Data Send\n");
  237.  
  238. Sleep(10);
  239. iResult = closesocket(sock);
  240. if (iResult == SOCKET_ERROR)
  241. {
  242. printf("Blad zamkniecia socketu = %d\n", WSAGetLastError() );
  243. WSACleanup();
  244. return 1;
  245. }
  246.  
  247.  
  248. WSACleanup();
  249. }
  250. return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement