Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #include <sys/wait.h>
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. #define PORT 7777
  16.  
  17. extern int errno;
  18.  
  19. void sighandler(int signo){
  20.     if(signo == SIGCHLD){
  21.         cout<<"[server-"<<getpid()<<"]Child terminated\n";
  22.         while(waitpid(-1, NULL, WNOHANG)>0);
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     struct sockaddr_in server;
  29.     struct sockaddr_in from;
  30.     string msg;
  31.     string answer;
  32.     char buf[1024];
  33.     int n = 0;
  34.     int sd;
  35.     int optval = 1;
  36.    
  37. //----->socket()
  38.     if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  39.     {
  40.         perror("[server]Socket() error.\n");
  41.         return errno;
  42.     }
  43.    
  44.     setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  45.    
  46.     bzero(&server, sizeof(server));
  47.     bzero(&from, sizeof(from));
  48.    
  49.     server.sin_family = AF_INET;
  50.     server.sin_addr.s_addr = htonl(INADDR_ANY);
  51.     server.sin_port = htons(PORT);
  52.    
  53. //---->bind()
  54.     if(bind(sd, (struct sockaddr*) &server, sizeof(struct sockaddr)) ==-1)
  55.     {
  56.         perror("[server]Bind() error.\n");
  57.         return errno;
  58.     }
  59.    
  60. //---->listen()
  61.     if(listen(sd, 5) == -1)
  62.     {
  63.         perror("[server]Listen() error.\n");
  64.         return errno;
  65.     }
  66.    
  67.     cout<<"[server-"<<getpid()<<"]Waiting at the port "<<PORT<<endl;
  68.    
  69.     signal(SIGCHLD, SIG_IGN);
  70.    
  71.     while(1)
  72.     {
  73.         int client;
  74.         socklen_t length = sizeof(from);
  75.         int pid; //procesul copil
  76.         char address[INET_ADDRSTRLEN]; // IP Client
  77.        
  78.         bzero(&from, sizeof(from));
  79.        
  80. //---->accept()
  81.         client=accept(sd, (struct sockaddr*) &from, &length);
  82.        
  83.         if(client<0)
  84.         {
  85.             perror("[server]Accept() error.\n");
  86.             continue;
  87.         }
  88.         else
  89.         {
  90.             inet_ntop(AF_INET, &from.sin_addr.s_addr, address, INET_ADDRSTRLEN);
  91.             cout<<"[server-"<<getpid()<<"]The client connected: ";
  92.             cout<<address<<':'<<ntohs(from.sin_port)<<endl;
  93.         }
  94.  
  95. //---->fork()
  96.         if((pid=fork())<0)
  97.         {
  98.             perror("fork() error");
  99.             continue;
  100.         }
  101.         else if(pid ==0)
  102.         {
  103.             msg.empty();
  104.             cout<<"[server-"<<getpid()<<"]Waiting a message...\n";
  105.  
  106.             bzero(buf, 500);
  107.  
  108.             while((n=recv(client, buf, sizeof(buf), 0))>0)
  109.             {
  110.                 msg.append(buf, buf+n);
  111.             }
  112.  
  113.             if(n<0)
  114.             {
  115.                 cout<<"[server-"<<getpid()<<"]Read from client error.\n";
  116.                 close(client);
  117.                 continue;
  118.             }
  119.  
  120.             cout<<"[server-"<<getpid()<<"]Message received: "<<msg<<endl;
  121.  
  122.             answer.empty();
  123.             answer="Hello "+msg;
  124.  
  125.             cout<<"[server-"<<getpid()<<"]Sending message back..."<<answer<<endl;
  126.  
  127.             if(n = (send(client, answer.data(), answer.size(), 0))<0)
  128.             {
  129.                 cout<<"[server-"<<getpid()<<"]Write to client error\n";
  130.                 continue;
  131.             }
  132.             else
  133.                 cout<<"[server-"<<getpid()<<"]Message successfully sent\n";
  134.  
  135.             close(client);
  136.  
  137.             exit(0);
  138.         } /*fork()*/
  139.  
  140.         close(client);
  141.    
  142.     }   /*while*/
  143. }   /*main*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement