Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/types.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <arpa/inet.h>
  11.  
  12.  
  13. int main(void)
  14. {
  15.     int sock = 0,n = 0;
  16.     char recvBuff[1024];
  17.     //TEST
  18.     char user_input[10];
  19.     char command[20];
  20.     struct sockaddr_in serv_addr;
  21.    
  22.     memset(recvBuff, '\0' ,sizeof(recvBuff));
  23.     if((sock = socket(AF_INET, SOCK_STREAM, 0))< 0)
  24.         {
  25.         printf("\n Error : Could not create socket \n");
  26.         return 1;
  27.         }
  28.    
  29.     serv_addr.sin_family = AF_INET;
  30.     serv_addr.sin_port = htons(5000);
  31.     //serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  32.     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  33.    
  34.     if(connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
  35.         {
  36.         printf("\n Error : Connect Failed \n");
  37.         return 1;
  38.         }
  39.     //printf("cmd (? for help)>");
  40.    
  41.     while(1)
  42.         {
  43.         printf("cmd (? for help)> ");
  44.         fgets(command, 20, stdin);
  45.         if (command[0] == '?')
  46.        printf("! You are connected to the server\n");
  47.         else
  48.        {
  49.        printf("cmd (? for help)>\n");
  50.        printf("Press a valid key\n");
  51.        }
  52.        
  53.         printf("! Please press a key:\n");
  54.         printf("! [1] list content of current directory (ls)\n! [2] print name of current directory (pwd)\n! [3] change current directory (cd)\n! [4] get file information\n! [5] display file (cat)\n! [?] this menu\n! [q] quit\ncmd (? for help)> ");
  55.        
  56.        
  57.         fgets(command, 20, stdin);
  58.         if (command[0] == '3')
  59.        {
  60.        printf("new dir (? for help)> ");
  61.        fgets(command, 20, stdin);
  62.        while (command[0] != '?')
  63.            {
  64.            printf("new dir (? for help)> ");
  65.            fgets(command, 20, stdin);
  66.            }
  67.        printf("! ..     the parent directory\n! / a new absolute directory\n!   a new directory relative to the current position\n! [?]    this menu\n! [q]    leave this menu\n");
  68.        printf("new dir (? for help)> ");
  69.        fgets(command, 20, stdin);
  70.        //printf("new dir (? for help)> %c%c", command[0], command[1]);
  71.        //printf("! OK\n");
  72.        }
  73.         write(sock, command, strlen(command));
  74.         memset(recvBuff, '\0', sizeof(recvBuff));
  75.         while ( (n = read(sock, recvBuff, sizeof(recvBuff)-1)) > 0)
  76.        {
  77.        printf("%s\n", recvBuff);
  78.        break;
  79.        }
  80.         }
  81.    
  82.    
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement