Advertisement
Guest User

cl3.c

a guest
Sep 4th, 2010
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define MSG_LEN 120
  10. #define DFL_PORT 8006
  11.  
  12. int get_cmd(char* line, int sock){
  13.     FILE *fp;
  14.     char* filename2 = malloc(strlen(line));
  15.     strcpy(filename2, line);
  16.     int isfileopen = 0;
  17.     int readcount;
  18.     char* filename = malloc(strlen(line));
  19.     filename = strtok(filename2, " ");
  20.     filename = strtok(NULL, " ");
  21.     printf("filename %s, line %s_\n", filename, line);
  22.  
  23.     if( (fp = fopen(filename, "wb")) == NULL ){
  24.         perror("get_cmd open file");
  25.         return 1;
  26.     }else{
  27.         isfileopen = 1;
  28.         write(sock, line, MSG_LEN);
  29.  
  30.     }
  31.  
  32.     while(1){
  33.         readcount = read(sock, line, MSG_LEN);
  34.         if(readcount<MSG_LEN) line[readcount] = '\0';
  35.         printf("get_cmd %s_%d\n", line, readcount);
  36.         fwrite(line, 1, readcount, fp);
  37.  
  38.             if(readcount<MSG_LEN-1){
  39.                 break;
  40.             }
  41.     }
  42.  
  43.     if(isfileopen){
  44.         fclose(fp);
  45.     }
  46.     printf("cl get_cmd: file recieved\n");
  47.  
  48. return 0;
  49. }
  50.  
  51.  
  52. int main(int argc, char *argv[]) {
  53.  
  54.   struct sockaddr_in server; //Server's address assembled here
  55.   struct hostent *host_info;
  56.   int sock, i;
  57.   char *server_name;
  58.   char* line = malloc(MSG_LEN);
  59.  
  60.  
  61. //   Get server name from commandline. If none, use localhost
  62.   if ( argc > 1 )
  63.     server_name = argv[1];
  64.   else
  65.     server_name = "localhost";
  66.  
  67. //   Create the socket
  68.   sock = socket(AF_INET, SOCK_STREAM, 0);
  69.   if (sock < 0) {
  70.     perror("creating stream socket");
  71.     exit(EXIT_FAILURE);
  72.   }
  73.  
  74.  
  75.   host_info = gethostbyname(server_name);
  76.   if (host_info == NULL) {
  77.     fprintf(stderr, "%s: unknown host: %s\n", argv[0], server_name);
  78.     exit(EXIT_FAILURE);
  79.   }
  80.  
  81.  
  82.   server.sin_family = host_info->h_addrtype;
  83.   memcpy((char *)&server.sin_addr, host_info->h_addr,host_info->h_length);
  84.   server.sin_port = htons(DFL_PORT);
  85.  
  86.   if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) {
  87.     perror("connecting to server");
  88.     exit(EXIT_FAILURE);
  89.   }
  90.  
  91.   printf("connected to server %s\n", server_name);
  92.   printf("Commands l - alias 'ls'\n         g [file name] - get file from server\n\n");
  93.  
  94. int command = 0;
  95. FILE *fp;
  96.  
  97. while(1){
  98.     int isfileopen = 0;
  99.  
  100.         fgets(line, MSG_LEN-1, stdin);
  101.  
  102.         command = line[0];
  103.     if(command == 'g'){
  104.         line[strlen(line)-1] = '\0';  // for deleting the '\n'
  105.                              // char that fgets put at the end
  106.     }
  107.  
  108.     printf("line %s_%d, x_%c_x\n", line, strlen(line), line[0]);
  109. //     for(i=0;i<strlen(line); i++){
  110. //
  111. //         printf("line[%d]: %c\n",i, line[i]);
  112. //     } // print out garbage
  113.    
  114.     if(line[0] == '\0'){
  115.         continue;
  116.     }
  117.  
  118.         printf("cmd %c\n", command);
  119.  
  120.         if(command == 'g'){
  121.             get_cmd(line, sock);
  122.             continue;
  123.         }
  124.  
  125.         write(sock, line, MSG_LEN-1);
  126.  
  127.  
  128.     printf("Get answer...\n");
  129.     while( (i=read(sock, line, MSG_LEN)) >0 ){
  130.         line[i] = '\0';
  131.         printf("%d_%s_\n",i, line);
  132.  
  133.  
  134.         if(i<MSG_LEN-1){
  135.             break;
  136.         }
  137.     }
  138.     bzero(line, MSG_LEN);
  139.         if(isfileopen){
  140.             fclose(fp);
  141.         }
  142. }
  143.   close(sock);
  144.   return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement