Advertisement
shaheer1999

Untitled

May 22nd, 2022
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.79 KB | None | 0 0
  1. #include <netinet/in.h>
  2. #include <pthread.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>
  11. #include <stdbool.h>
  12.  
  13. #define BACKLOG 10
  14. #define MAX_SIZE 200
  15. #define BACKLOG 10
  16. #define BUFFSIZE 2048
  17. #define MAXPENDING 5
  18. #define MAX 2048
  19.  
  20. typedef struct server_arg {
  21.     int portNum;
  22. } server_arg;
  23. typedef struct server_arg1 {
  24.     int portNum;
  25. } server_arg1;
  26.  
  27. void setup(char inputBuffer[], char *args[], int *background)
  28. {
  29.     const char s[4] = " \t\n";
  30.     char *token;
  31.     token = strtok(inputBuffer, s);
  32.     int i = 0;
  33.     while( token != NULL)
  34.     {
  35.         args[i] = token;
  36.         i++;
  37.         //printf("%s\n", token);
  38.         token = strtok(NULL,s);
  39.     }
  40.     args[i] = NULL;
  41. }
  42.    
  43. void *terminal_thread(void *arg) {
  44.  while(1) {
  45. printf(">> ");
  46. //memset(input_command,0,strlen(str));
  47. char *input_command = malloc(MAX_SIZE);
  48.  
  49. fgets(input_command,MAX_SIZE,stdin);
  50.  
  51. if((strlen(input_command) > 0) && (input_command[strlen(input_command) - 1] == '\n'))
  52.   input_command[strlen(input_command) - 1] = '\0';
  53.  
  54. char list[] = "ls";
  55. char cp[]= "cp";
  56.  
  57. /*char s[100];
  58.  
  59.   printf("%s\n", getcwd(s,100));
  60.  
  61.   chdir("Desktop");
  62.  
  63.   printf("%s\n", getcwd(s,100));
  64. */
  65.  
  66.  
  67. if(strcmp(input_command,list) == 0) {
  68.   //ls code will run here
  69.    
  70.  
  71. }
  72. if (strchr(input_command,'.') != NULL && strchr(input_command,'l') !=NULL) {
  73.    
  74.      printf("remote ls\n");
  75.    char ip[20];
  76.    const char c[2] = " ";
  77.    
  78.    //strcpy(str,input_command);  
  79.    char *token;
  80.        
  81.    // get the first token
  82.     token = strtok(input_command, c);
  83.        
  84.     //walk through other tokens
  85.     int i = 0;
  86.     while( token != NULL && i !=-1 ) {
  87.      
  88.       token = strtok(NULL, c);
  89.       i--;
  90.      
  91.    }
  92.       strcpy(ip,token);
  93.      
  94.         int sock;
  95.         struct sockaddr_in echoserver;
  96.         char buffer[BUFFSIZE];
  97.         unsigned int echolen;
  98.         int received = 0;
  99.        
  100.         if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  101.         {
  102.             perror("Failed to create socket");
  103.             exit(1);
  104.         }
  105.        int enable = 1;
  106.         if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
  107.           perror("error");
  108.         }
  109.         memset(&echoserver, 0, sizeof(echoserver));
  110.         echoserver.sin_family = AF_INET;
  111.         echoserver.sin_addr.s_addr = inet_addr(ip);
  112.         echoserver.sin_port = htons(5126);
  113.         if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0)
  114.         {
  115.             perror("Failed to connect with server");
  116.             exit(1);
  117.         }
  118.         char s[100];
  119.    // while(1) { // to repeat the whole process until exit is typed
  120.         strcpy(s, "ls");
  121.         s[strlen(s)-1]='\0'; //fgets doesn't automatically discard '\n'
  122.         echolen = strlen(s);
  123.  
  124.         /*  send() from client; */
  125.         if (send(sock, s, echolen, 0) != echolen)
  126.         {
  127.             perror("Mismatch in number of sent bytes");
  128.         }
  129.  
  130.         fprintf(stdout, "Message from server: ");
  131.        
  132.             int bytes = 0;
  133.             /* recv() from server; */
  134.             if ((bytes = recv(sock, buffer, echolen, 0)) < 1)
  135.             {
  136.                 perror("Failed to receive bytes from server");
  137.             }
  138.             received += bytes;
  139.             buffer[bytes] = '\0';
  140.             /* Assure null terminated string */
  141.             fprintf(stdout, buffer);
  142.        
  143.         bytes = 0;
  144. // this d {...} while block will receive the buffer sent by server
  145.         do {
  146.             buffer[bytes] = '\0';
  147.             printf("%s\n", buffer);
  148.         } while((bytes = recv(sock, buffer, BUFFSIZE-1, 0))>=BUFFSIZE-1);
  149.         buffer[bytes] = '\0';
  150.         printf("%s\n", buffer);
  151.         printf("\n");
  152.        
  153.          continue;
  154.           }
  155.    }
  156. }
  157.  
  158. void  *server_socket_ls(void *arg) {
  159. int* exit_status = (int*)malloc(sizeof(int));
  160. *exit_status = 0;  
  161. while (*exit_status == 0) {
  162. server_arg *s = (server_arg*)arg;
  163. int server_fd, new_socket;
  164.     struct sockaddr_in address;
  165.     int addrlen = sizeof(address);
  166.    
  167.  
  168.     // Creating socket file descriptor
  169.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
  170.         == 0) {
  171.         perror("socket failed");
  172.         exit(EXIT_FAILURE);
  173.     }
  174.         int enable = 1;
  175.         if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
  176.           perror("error");
  177.         }
  178.  
  179.     address.sin_family = AF_INET;
  180.     address.sin_addr.s_addr = INADDR_ANY;
  181.     address.sin_port = htons(s->portNum);
  182.  
  183.  
  184.     if (bind(server_fd, (struct sockaddr*)&address,
  185.              sizeof(address))
  186.         < 0) {
  187.         perror("bind failed");
  188.        
  189.     }
  190.     printf("About to listen");
  191.     if (listen(server_fd, 3) < 0) {
  192.         perror("listen");
  193.        
  194.     }
  195.    
  196.     if ((new_socket
  197.          = accept(server_fd, (struct sockaddr*)&address,
  198.                   (socklen_t*)&addrlen))
  199.         < 0) {
  200.         perror("accept");
  201.     }
  202.    
  203.     printf("Server Connected\n");
  204.    
  205.    
  206. //code for ls
  207.    
  208. char buffer[BUFFSIZE];
  209. int received = -1;
  210. char data[MAX];
  211. memset(data,0,MAX);
  212.    // this will make server wait for another command to run until it receives exit
  213.     data[0] = '\0';
  214.     if((received = recv(new_socket, buffer,BUFFSIZE,0))<0){
  215.  
  216.         perror("Failed");
  217.     }
  218.  
  219.     buffer[received] = '\0';
  220.  
  221.     strcat (data,  buffer);
  222.     if (strcmp(data, "exit")==0) // this will force the code to exit
  223.         exit(0);
  224.  
  225.     puts (data);
  226.         char *args[100];
  227.         setup(data,args,0);
  228.         int pipefd[2],lenght;
  229.  
  230.         if(pipe(pipefd))
  231.             perror("Failed to create pipe");
  232.  
  233.         pid_t pid = fork();
  234.         char path[MAX];
  235.  
  236.         if(pid==0)
  237.         {
  238.             close(1); // close the original stdout
  239.             dup2(pipefd[1],1); // duplicate pipfd[1] to stdout
  240.             close(pipefd[0]); // close the readonly side of the pipe
  241.             close(pipefd[1]); // close the original write side of the pipe
  242.             execvp(args[0],args); // finally execute the command
  243.         }
  244.         else
  245.             if(pid>0)
  246.             {
  247.                 close(pipefd[1]);
  248.                 memset(path,0,MAX);
  249.  
  250.                 while(lenght=read(pipefd[0],path,MAX-1)){
  251.                     printf("Data read so far %s\n", path);
  252.                     if(send(new_socket,path,strlen(path),0) != strlen(path) ){
  253.                         perror("Failed");
  254.                     }
  255.                     //fflush(NULL);
  256.                     printf("Data sent so far %s\n", path);
  257.                     memset(path,0,MAX);
  258.                    
  259.                 }
  260.  
  261.                 //close(pipefd[0]);
  262.                 //removed so server will not terminate
  263.                          
  264.             }
  265.             else
  266.             {
  267.                 printf("Error !\n");
  268.                 exit(0);
  269.             }
  270.              printf("Enter 1 if you want to exit or 0 if you don't: ");
  271.              fgets(exit_status,MAX_SIZE,stdin);    
  272.            
  273. }
  274.  
  275. }
  276.  
  277. void  *server_socket_file(void *arg) {
  278.  
  279. server_arg1 *s1 = (server_arg1*)arg;
  280. int server_fd, new_socket;
  281.     struct sockaddr_in address;
  282.     int addrlen = sizeof(address);
  283.    
  284.  
  285.     // Creating socket file descriptor
  286.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))
  287.         == 0) {
  288.         perror("socket failed");
  289.         exit(EXIT_FAILURE);
  290.     }
  291.         int enable = 1;
  292.         if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) < 0) {
  293.           perror("error");
  294.         }
  295.  
  296.     address.sin_family = AF_INET;
  297.     address.sin_addr.s_addr = INADDR_ANY;
  298.     address.sin_port = htons(s1->portNum);
  299.  
  300.  
  301.     if (bind(server_fd, (struct sockaddr*)&address,
  302.              sizeof(address))
  303.         < 0) {
  304.         perror("bind failed");
  305.        
  306.     }
  307.     if (listen(server_fd, 3) < 0) {
  308.         perror("listen");
  309.        
  310.     }
  311.     if ((new_socket
  312.          = accept(server_fd, (struct sockaddr*)&address,
  313.                   (socklen_t*)&addrlen))
  314.         < 0) {
  315.         perror("accept");
  316.     }
  317.    
  318.     printf("Server Connected\n");
  319.    
  320.  
  321. }
  322.  
  323. int main(int argc, char const* argv[])
  324. {
  325.  
  326.  
  327.   server_arg *s = (server_arg*)malloc(sizeof(server_arg));
  328.   server_arg1 *s1 = (server_arg1*)malloc(sizeof(server_arg1));
  329.   pthread_t id_1;
  330.   pthread_t id_2;
  331.   pthread_t id_3;
  332.   if (pthread_create(&id_3, NULL,(void *) terminal_thread, NULL) != 0) {
  333.             perror("pthread_create");  
  334.         }
  335.  
  336.   s->portNum = 9191;
  337.   pthread_create(&id_1,NULL,(void *)server_socket_ls,(void *)s);
  338.   s1->portNum = 6123;
  339.   pthread_create(&id_2,NULL,(void *)server_socket_file,(void *)s1);
  340.  
  341.  
  342.   pthread_join(id_1,NULL);
  343.   pthread_join(id_2,NULL);
  344.   pthread_join(id_3,NULL);
  345.   pthread_exit(0);
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement