Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #include <string.h>
- #include <windows.h>
- #include <time.h>
- #define SERVER "SK_ProjektSerwer/1.0"
- #define PROTOCOL "HTTP/1.1"
- #define RFC1123FMT "%a, %d %b %Y %H:%M:%S"
- #define PORT 80
- int main()
- {
- FILE *plik, *plikbuf, *godz;
- WSADATA wsaData;
- WORD wVersionRequested = MAKEWORD(2, 2);
- SOCKET sock;
- SOCKET AcceptSock;
- fd_set fds;
- char timebuf[128];
- time_t now;
- char bufor[4096];
- char *sciezka, *metoda, *protokol;
- char sendBufor[50000], sendHeader[50000], sendHTML[50000];
- struct sockaddr_in adres;
- char *adresS;
- struct hostent *remoteHost;
- int length=0, i=0;
- while(1)
- {
- // WSASTARTUP
- int err = WSAStartup(wVersionRequested, &wsaData);
- if (err != 0)
- {
- printf("WSAStartup blad numer: %d\n", err);
- return 1;
- }
- sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock == INVALID_SOCKET)
- printf("Blad funkcji socket = %d\n", WSAGetLastError() );
- else
- {
- printf("Funkcja socket wykonana poprawnie\n");
- }
- adres.sin_addr.s_addr=inet_addr("127.0.0.1");
- adres.sin_family=AF_INET;
- adres.sin_port=htons(PORT);
- int iResult = bind(sock, (SOCKADDR *) &adres, sizeof (adres));
- if (iResult == SOCKET_ERROR)
- {
- printf("Blad funkcji bind nr %u\n", WSAGetLastError());
- closesocket(sock);
- WSACleanup();
- return 1;
- }
- else
- printf("Poprawnie wykonano funkcje bind\n");
- if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
- printf("listen function failed with error: %d\n", WSAGetLastError());
- printf("Listening on socket...\n");
- AcceptSock = accept(sock, NULL, NULL);
- if (AcceptSock == INVALID_SOCKET)
- {
- printf("accept failed with error: %d\n", WSAGetLastError());
- closesocket(sock);
- WSACleanup();
- return 1;
- }
- else
- printf("Client connected.\n\n");
- memset(bufor,0,sizeof(bufor));
- iResult = recv(AcceptSock,bufor, sizeof(bufor), 0);
- if (iResult == SOCKET_ERROR)
- {
- printf("Blad funkcji recvfrom nr: %d\n", WSAGetLastError());
- }
- printf("%s\n",bufor);
- metoda = strtok(bufor,"/");
- printf("%s\n",metoda);
- sciezka = strtok(NULL," ");
- printf("%s\n",sciezka);
- protokol = strtok(NULL,"\r");
- printf("%s\n",protokol);
- if(!strcmp(protokol,PROTOCOL))
- {
- // ******************* METODA GET ********************
- if (!strncmp(metoda, "GET", 3))
- {
- if(!strcmp(sciezka,"godzina.html"))
- {
- //*********** Dynamiczne generowanie strony z godzina ****************
- godz = fopen("godzina.html","w");
- now = time(NULL);
- strftime(timebuf, sizeof(timebuf), "%H:%M:%S", localtime(&now));
- fprintf(godz, "<html>\n<head>\n</head>\n<body>\n<center><i><b>Jest godzina: %s</b></i></center>\n</body>\n</html>", timebuf);
- fclose(godz);
- }
- plik = fopen(sciezka,"rt");
- if(plik!=NULL)
- {
- plikbuf = fopen("dane.txt","wt");
- memset(sendHeader,0,sizeof(sendHeader));
- memset(sendHTML,0,sizeof(sendHTML));
- memset(sendBufor,0,sizeof(sendBufor));
- fprintf(plikbuf, "%s 200 OK\r\n", PROTOCOL);
- now = time(NULL);
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
- fprintf(plikbuf, "Date: %s\r\n", timebuf);
- fprintf(plikbuf, "Server: %s\r\n", SERVER);
- fprintf(plikbuf, "Content-Type: text/html \r\n");
- length = fread(sendHTML,1, sizeof(sendHTML), plik);
- fprintf(plikbuf, "Content-Length: %d \r\n", length);
- fprintf(plikbuf, "Connection: close \r\n");
- fprintf(plikbuf, "\r\n");
- fclose(plikbuf);
- plikbuf = fopen("dane.txt","rt");
- fread(sendHeader,1, sizeof(sendHeader), plikbuf);
- strcat(sendHeader,sendHTML);
- strcpy(sendBufor,sendHeader);
- fclose(plikbuf);
- fclose(plik);
- }
- else
- {
- //************************ Jesli nie znaleziono pliku ******************
- plikbuf = fopen("dane.txt","wt");
- memset(sendHeader,0,sizeof(sendHeader));
- memset(sendBufor,0,sizeof(sendBufor));
- fprintf(plikbuf, "%s 404 Not Found\r\n",PROTOCOL);
- now = time(NULL);
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
- fprintf(plikbuf, "Date: %s\r\n", timebuf);
- fprintf(plikbuf, "Server: %s\r\n", SERVER);
- fprintf(plikbuf, "Connection: close \r\n");
- fprintf(plikbuf, "\r\n");
- fclose(plikbuf);
- plikbuf = fopen("dane.txt","rt");
- fread(sendHeader,1, sizeof(sendHeader), plikbuf);
- strcpy(sendBufor,sendHeader);
- fclose(plikbuf);
- }
- }
- //**********************METODA HEAD******************
- else if(!strncmp(metoda, "HEAD", 4))
- {
- memset(sendBufor,0,sizeof(sendBufor));
- strcpy(sendBufor,"Zapytanie HEAD jeszcze nie oprogramowane") ;
- }
- else
- {
- //*********************METODA NIEPRAWIDLOWA***********
- plikbuf = fopen("dane.txt","wt");
- memset(sendHeader,0,sizeof(sendHeader));
- memset(sendBufor,0,sizeof(sendBufor));
- fprintf(plikbuf, "HTTP Error 405 Method not allowed\r\n");
- now = time(NULL);
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
- fprintf(plikbuf, "Date: %s\r\n", timebuf);
- fprintf(plikbuf, "Server: %s\r\n", SERVER);
- fprintf(plikbuf, "Connection: close \r\n");
- fprintf(plikbuf, "\r\n");
- fclose(plikbuf);
- plikbuf = fopen("dane.txt","rt");
- fread(sendHeader,1, sizeof(sendHeader), plikbuf);
- strcpy(sendBufor,sendHeader);
- fclose(plikbuf);
- }
- }
- else
- {
- //******************** ZĀ£Y PROTOKOL **********************
- plikbuf = fopen("dane.txt","wt");
- memset(sendHeader,0,sizeof(sendHeader));
- memset(sendBufor,0,sizeof(sendBufor));
- fprintf(plikbuf, "HTTP 505 Version Not Supported\r\n");
- now = time(NULL);
- strftime(timebuf, sizeof(timebuf), RFC1123FMT, localtime(&now));
- fprintf(plikbuf, "Date: %s\r\n", timebuf);
- fprintf(plikbuf, "Server: %s\r\n", SERVER);
- fprintf(plikbuf, "Connection: close \r\n");
- fprintf(plikbuf, "\r\n");
- fclose(plikbuf);
- plikbuf = fopen("dane.txt","rt");
- fread(sendHeader,1, sizeof(sendHeader), plikbuf);
- strcpy(sendBufor,sendHeader);
- fclose(plikbuf);
- }
- printf("\n**************Wyslalem******************\n\n%s\n",sendBufor);
- printf("\n***************************************\n");
- if( send(AcceptSock , sendBufor , strlen(sendHeader)+length , 0) < 0)
- {
- puts("Send failed");
- return 1;
- }
- puts("Data Send\n");
- Sleep(10);
- iResult = closesocket(sock);
- if (iResult == SOCKET_ERROR)
- {
- printf("Blad zamkniecia socketu = %d\n", WSAGetLastError() );
- WSACleanup();
- return 1;
- }
- WSACleanup();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement