Advertisement
gitarnik

client.c

Oct 24th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #define closesocket close
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. #define PROTOPORT 5193
  13. extern int errno;
  14.  
  15. char localhost[] = "localhost";
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.   struct hostent *ptrh;
  21.   struct protoent *ptrp;
  22.   struct sockaddr_in sad;
  23.   int sd;
  24.   int port;
  25.   char *host;
  26.   int n;
  27.   char buf[1000];
  28.  
  29.   memset((char *)&sad, 0, sizeof(sad));
  30.   sad.sin_family = AF_INET;
  31.  
  32.   if (argc > 2)
  33.     {
  34.       port = atoi(argv[2]);
  35.     } else port=PROTOPORT;
  36.   if (port > 0)
  37.     {
  38.       sad.sin_port = htons((u_short)port);
  39.     }
  40.   else
  41.     {
  42.       fprintf(stderr, "Invalid port number %s\n",argv[2]);
  43.       return 1;
  44.     }
  45.  
  46.   if (argc > 1) host=argv[1]; else host=localhost;
  47.  
  48.   ptrh=gethostbyname(host);
  49.   if ((char *)ptrh==NULL)
  50.     {
  51.       fprintf(stderr, "Invalid host name %s\n",argv[1]);
  52.       return 1;
  53.     }
  54.  
  55.   memcpy(&sad.sin_addr,ptrh->h_addr,ptrh->h_length);
  56.  
  57.   if (((long int)(ptrp=getprotobyname("tcp")))==0)
  58.     {
  59.       fprintf(stderr, "Can't change TCP number\n");
  60.       return 1;
  61.     }
  62.  
  63.   sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
  64.  
  65.   if (sd < 0)
  66.     {
  67.       fprintf(stderr, "Can't create socket\n");
  68.       return 1;
  69.     }
  70.    
  71.   if (connect(sd, (struct sockaddr *)&sad, sizeof(sad))<0)
  72.     {
  73.       fprintf(stderr, "Can't connect to socket\n");
  74.       return 1;
  75.     }
  76.  
  77.   int liczba1;
  78.   printf("Podaj pierwszą liczbę");
  79.   scanf("%d", liczba1);
  80.  
  81.   int liczba2;
  82.   printf("Podaj drugą liczbę");
  83.   scanf("%d", liczba2);
  84.  
  85.   char *tekst;
  86.   printf("Podaj operację (+, -, *, /)");
  87.   scanf("%d", tekst);
  88.  
  89.   send(sd,liczba1,strlen(liczba1),0);
  90.   send(sd,liczba2,strlen(liczba2),0);
  91.   send(sd,operacja,strlen(operacja),0);
  92.  
  93.   n=recv(sd,buf,sizeof(buf),0);
  94.   while (n>0)
  95.     {
  96.       write(1,buf,n);
  97.       n=recv(sd,buf,sizeof(buf),0);
  98.     }
  99.  
  100.   closesocket(sd);
  101.  
  102.   return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement