SHARE
TWEET

Untitled

a guest Feb 19th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <signal.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <sys/un.h>
  10. #include <string>
  11. #include <fstream>
  12. #include <iostream>
  13.  
  14. using namespace std;
  15.  
  16. const int BUFFER_SIZE = 1024;
  17. struct sockaddr_in serv_addr;
  18. struct sockaddr_in cli_addr;
  19. int sockInet;           //Socket
  20. int newSockInet;        //New socket descriptor
  21.  
  22. string findWords(string words, string fileName) {
  23.     ifstream inputFile(fileName.c_str());
  24.     string line;
  25.     string result = "";
  26.     while (getline(inputFile, line)) {
  27.         if (line.find(words) != string::npos) {
  28.         result.append(line);
  29.         result.append("\n");
  30.             }
  31.         }
  32.         inputFile.close();
  33.         return result;
  34. }
  35.  
  36. string acceptString(string socketName){
  37.     int sockfd, servlen,n;
  38.         struct sockaddr_un  serv_addr;      
  39.  
  40.     bzero((char *)&serv_addr,sizeof(serv_addr));
  41.         serv_addr.sun_family = AF_UNIX;
  42.         socketName.copy(serv_addr.sun_path, socketName.size());
  43.         servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
  44.         if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
  45.             printf("Error creating socket client\n");
  46.         while (true) {
  47.             if (connect(sockfd, (struct sockaddr *) &serv_addr, servlen) >= 0)
  48.                     break;
  49.         }
  50.         char buffer[BUFFER_SIZE];
  51.         n=read(sockfd, buffer, BUFFER_SIZE);
  52.         close(sockfd);
  53.         shutdown(sockfd,1);  
  54.         return string(buffer);
  55. }
  56.  
  57. void sendString(string socketName, string toSend) {
  58.         int sockfd, newsockfd, servlen;
  59.         socklen_t clilen;
  60.         struct sockaddr_un  cli_addr, serv_addr;
  61.    
  62.         if ((sockfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
  63.             printf("error on creating socket Server \n");
  64.        
  65.         bzero((char *) &serv_addr, sizeof(serv_addr));
  66.         serv_addr.sun_family = AF_UNIX;
  67.         socketName.copy(serv_addr.sun_path, socketName.size());
  68.         servlen=strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
  69.    
  70.         if(bind(sockfd,(struct sockaddr *)&serv_addr,servlen)<0)
  71.             printf("error on binding socket Server \n");
  72.         listen(sockfd,5);
  73.         clilen = sizeof(cli_addr);
  74.         newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
  75.         if (newsockfd < 0)
  76.                 printf("error accepting Server \n");
  77.  
  78.         const char* buffer = toSend.c_str();
  79.         char newBuffer[BUFFER_SIZE];          
  80.         strcpy(newBuffer, buffer);
  81.         newBuffer[toSend.size()] = '\0';
  82.         int n = write(newsockfd, newBuffer, toSend.size()+1);
  83.  
  84.         shutdown(newsockfd,1);
  85.         close(newsockfd);
  86.         shutdown(sockfd,1);
  87.         close(sockfd);
  88. }
  89.  
  90. void work(string socketName, string words, string fileName) {
  91.     string found = findWords(words, fileName);
  92.     sendString(socketName, found);
  93. }
  94.  
  95. int main(int argc, char* argv[]){
  96.     //Create and check socket
  97.     sockInet = socket(AF_INET, SOCK_STREAM, 0);
  98.     if (sockInet < 0) {
  99.         fprintf(stderr, "Server: ERROR opening socket\n");
  100.         return 0;
  101.     }
  102.    
  103.     //Setting up sockaddr
  104.     serv_addr.sin_family = AF_INET;         //Set adress family
  105.     serv_addr.sin_port = htons(8000);       //Convert and set port
  106.     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);  //Connect to any
  107.  
  108.     if (bind(sockInet, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  109.         fprintf(stderr, "Server: ERROR on binding\n");
  110.         return 0;
  111.     }
  112.    
  113.     //Set socket to listen state
  114.     listen(sockInet, 1);
  115.  
  116.     //Accept connection
  117.     socklen_t clilen = sizeof(cli_addr);
  118.         newSockInet = accept(sockInet,(struct sockaddr *) &cli_addr, &clilen);
  119.        
  120.     if (newSockInet < 0) {
  121.             fprintf(stderr, "Server: ERROR connecting\n");
  122.             return 0;
  123.         } else {
  124.         fprintf(stdout, "Server: Client connected\n");
  125.     }
  126.  
  127.     char buf[BUFFER_SIZE];
  128.     bzero(buf, BUFFER_SIZE);
  129.  
  130.     int count = read(newSockInet, buf, BUFFER_SIZE);
  131.         string wordToFind = string(buf).substr(0, count - 1);
  132.     fprintf(stdout, "Server: String accepted \"%s\"\n", wordToFind.c_str());
  133.     if (fork() == 0) {          //Worker1
  134.         work("socket1", wordToFind, "file1.txt");
  135.     } else {
  136.         if (fork() == 0) {      //Worker2
  137.             work("socket2", wordToFind, "file2.txt");
  138.         } else {
  139.             if (fork() == 0) {  //Worker3
  140.                 work("socket3", wordToFind, "file3.txt");
  141.             } else {        //Parent
  142.                 string result = acceptString("socket1");
  143.                 result.append(acceptString("socket2"));
  144.                 result.append(acceptString("socket3"));
  145.                
  146.                 const char* buffer = result.c_str();
  147.                         char newBuffer[BUFFER_SIZE];          
  148.                         strcpy(newBuffer, buffer);
  149.                         newBuffer[result.size()] = '\0';
  150.                         int n = write(newSockInet, newBuffer, result.size() + 1);
  151.                
  152.                 shutdown(newSockInet, SHUT_RDWR);
  153.                 shutdown(sockInet, SHUT_RDWR);
  154.                 close(newSockInet);
  155.                 close(sockInet);
  156.             }
  157.         }
  158.     }
  159.  
  160.     remove("socket1");
  161.     remove("socket2");
  162.     remove("socket3");
  163.     return 0;
  164. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top