Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: C  |  size: 1.79 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <netdb.h>
  9.  
  10. typedef struct order{
  11.         char command;
  12.         int param1;
  13.         int param2;
  14.  
  15. } ORDER;
  16. int main(int argc, char *argv[]){
  17.         int s;
  18.         struct sockaddr_in sa;
  19.         struct hostent *hp;
  20.         char newline = '\n';
  21.         ORDER o ;
  22.         if(argc<2){
  23.                 puts("you must provide a user");
  24.                 return EXIT_FAILURE;
  25.         }
  26.         char c;
  27.         int a,b;
  28.  
  29.         if(scanf("%c %d %d", &c , &a,&b) < 3){
  30.                 printf("you need to provide command(+-*/) and two integer numbers");   
  31.                 return EXIT_FAILURE;
  32.         }
  33.  
  34.         o.command = c;
  35.         o.param1 = a;
  36.         o.param2 = b;
  37.  
  38.  
  39.         if( (hp = gethostbyname("localhost")) == NULL ){
  40.                 perror("failed to translate the hostname");    
  41.                 return EXIT_FAILURE;
  42.  
  43.         }
  44.  
  45.         bzero( (char *) &sa, sizeof(sa));      
  46.         sa.sin_family=AF_INET;
  47.         bcopy( (char *) hp->h_addr, (char *) &sa.sin_addr.s_addr, hp->h_length);
  48.  
  49.         if( (s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0 ){
  50.                 perror("failed creating the socket");
  51.                 return EXIT_FAILURE;
  52.         }
  53.  
  54.         sa.sin_port=htons(atoi(argv[2]));
  55.  
  56.         if(connect(s,(struct sockaddr *) &sa, sizeof(sa)) < 0 ){
  57.                 perror("an error has occured while connecting");
  58.                 return EXIT_FAILURE;
  59.         }
  60.  
  61.  
  62.  
  63.         if(full_send(s,&o,sizeof(o))<0){
  64.                 perror("an error has occured while sending the data"); 
  65.                 return EXIT_FAILURE;
  66.         }
  67.  
  68.  
  69.          if ( recv(s, &a,sizeof(a),0) != sizeof(a) ){
  70.                 perror("error while receiving the result");
  71.                 return EXIT_FAILURE;
  72.          }
  73.  
  74.         printf("%d", a);
  75.  
  76.         close(s);
  77.  
  78.         return EXIT_SUCCESS;
  79. }
  80.  
  81. int full_send(int socket, void* buffer, size_t size){
  82.         size_t sent =0 ;
  83.         size_t ret;
  84.         while(sent < size){
  85.                 ret = send(socket, buffer+sent, size-sent,0);
  86.                 if ( ret < 0 ){
  87.                         return -1;
  88.                 }else{
  89.                         sent +=ret;
  90.                 }
  91.  
  92.         }
  93.  
  94.         return sent;
  95.  
  96. }