Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

server

a guest Nov 17th, 2018 64 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /* The port number is passed as an argument */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. //#include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <iostream>
  11. #include <sys/wait.h>
  12. #include <signal.h>
  13. #include <sstream>
  14. #include <fstream>
  15.  
  16.  
  17. #define RCVBUFSIZE 50 /* Size of receive buffer */
  18. using namespace std;
  19.  
  20. void error(const char *msg)
  21. {
  22.     perror(msg);
  23.     exit(1);
  24. }
  25.  
  26. void get(int clntSocket, string fileName) {
  27.     /*sending protocol of files
  28.      * check if the file is found and send response
  29.      * if found send the file as arrays
  30.      */
  31.     FILE *file = fopen(fileName.c_str(), "rb");
  32.     if (file == NULL) {//file not found
  33.  
  34.         string s = "HTTP/1.0 404 Not Found\r\n";
  35.         char buffer[s.size() + 1];
  36.         strncpy(buffer, s.c_str(), sizeof(buffer));
  37.         buffer[sizeof(buffer) - 1] = 0;
  38.         send(clntSocket, buffer, sizeof(buffer), 0);
  39.  
  40.         return;
  41.     }
  42.  
  43.     //file found
  44.     string s = "HTTP/1.0 200 OK\r\n";
  45.     char buffer[1024];
  46.     strncpy(buffer, s.c_str(), sizeof(buffer));
  47.     buffer[sizeof(buffer)-1] = 0;
  48.     if (send(clntSocket, buffer, sizeof(buffer), 0) != sizeof(buffer))
  49.         error("send() failed");
  50.     int read_size;
  51.     char send_buffer[1024];
  52.     while (!feof(file)) {
  53.         read_size = fread(send_buffer, 1, 1024 , file);
  54.         cout<<read_size<<endl;
  55.         if (send(clntSocket, send_buffer, read_size, 0) != read_size)
  56.             error("send() failed");
  57.     }
  58.     fclose(file);
  59. }
  60.  
  61. void post(int clntSocket, string fileName) {
  62.     //create file with fileName
  63.     FILE* file=fopen(fileName.c_str(), "wb");
  64.     int recv_size;
  65.     char buffer[1024];
  66.     if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
  67.                 error("recv() failed");
  68.     while (recv_size > 0) {
  69.         fwrite(buffer, 1, recv_size, file);
  70.         if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
  71.             error("recv() failed");
  72.     }
  73.     fclose(file);
  74. }
  75.  
  76. void HandleTCPClient(int clntSocket) {
  77.     char echoBuffer[RCVBUFSIZE];
  78.     int recvMsgSize;
  79.     /* Buffer for echo string */
  80.     /* Size of received message */
  81.     /* Receive message from client */
  82.     if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
  83.         error("recv() failed");
  84.     cout<<echoBuffer<<endl;
  85.     //split the message and find the method and the fileName
  86.     istringstream iss(echoBuffer);
  87.     string type, fileName;
  88.     iss >> type;
  89.     iss >> fileName;
  90.     cout << "type: " << type << endl;
  91.     cout << "filename: " << fileName << endl;
  92.     if (type == "GET" || type == "get")
  93.         get(clntSocket, fileName);
  94.     else if(type == "POST" || type == "post")
  95.         post(clntSocket, fileName);
  96.     else
  97.         error("incorrect type");
  98.     //close(clntSocket); /* Close client socket */
  99. }
  100.  
  101.  
  102. void sigchld_handler(int s)
  103. {
  104.     while(wait(NULL) > 0);
  105. }
  106.  
  107. int main(int argc, char *argv[])
  108. {
  109.      int sockfd, newsockfd, portno;
  110.      socklen_t clilen;
  111.      char buffer[256];
  112.      struct sockaddr_in serv_addr, cli_addr;
  113.      int n;
  114.      struct sigaction sa;
  115.      /*if (argc < 2) {
  116.          fprintf(stderr,"ERROR, no port provided\n");
  117.          exit(1);
  118.      }*/
  119.  
  120.      sockfd =  socket(AF_INET, SOCK_STREAM, 0);
  121.      if (sockfd < 0)
  122.         error("ERROR opening socket");
  123.  
  124.      bzero((char *) &serv_addr, sizeof(serv_addr));
  125.  
  126.      //portno = atoi(argv[1]);
  127.      portno = 5000;
  128.  
  129.      serv_addr.sin_family = AF_INET;
  130.  
  131.      serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //*/*//
  132.  
  133.      serv_addr.sin_port = htons(portno);
  134.  
  135.      if (bind(sockfd, (struct sockaddr *) &serv_addr,
  136.               sizeof(serv_addr)) < 0)
  137.               error("ERROR on binding");
  138.  
  139.      if (listen(sockfd,5) < 0)
  140.         error("ERROR on listening");
  141.  
  142.     sa.sa_handler = sigchld_handler; // reap all dead processes
  143.     sigemptyset(&sa.sa_mask);
  144.     sa.sa_flags = SA_RESTART;
  145.     if (sigaction(SIGCHLD, &sa, NULL) == -1)
  146.     {
  147.         perror("sigaction");
  148.         exit(1);
  149.     }
  150.  
  151.     while(1) {
  152.      // The accept() call actually accepts an incoming connection
  153.      clilen = sizeof(cli_addr);
  154.     cout << "before accept"<<endl;
  155.      if ((newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen)) < 0)
  156.      {
  157.      continue;
  158.           //error("ERROR on accept");
  159.      }
  160.       cout << "connections was found"<< endl;
  161.     if (!fork())
  162.         { // this is the child process
  163.             close(sockfd); // child doesn.t need the listener
  164.             HandleTCPClient(newsockfd);
  165.             close(newsockfd);
  166.             exit(0);
  167.         }
  168.     close(newsockfd); // parent doesn.t need this
  169.     }
  170.      return 0;
  171. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top