daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest May 21st, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //client.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <errno.h>
  7. #include <error.h>
  8. #include <netinet/in.h>
  9. #include <netinet/ip.h>
  10. #include <arpa/inet.h>
  11. #include <string.h>
  12. #include <signal.h>
  13.  
  14. void str_cli(FILE *fp, int sfd ) {
  15.     char sendline[1024], recvline[2014];
  16.     memset(recvline, 0, sizeof(sendline));
  17.     memset(sendline, 0, sizeof(recvline));
  18.     while( fgets(sendline, 1024, fp) != NULL ) {
  19.         write(sfd, sendline, strlen(sendline));
  20.         if( read(sfd, recvline, 1024) == 0 ) {
  21.             printf("server term prematurely.\n");
  22.         }
  23.         fputs(recvline, stdout);
  24.         memset(recvline, 0, sizeof(sendline));
  25.         memset(sendline, 0, sizeof(recvline));
  26.     }
  27. }
  28.  
  29. int main() {
  30.     int s;
  31.     if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
  32.         int e = errno;
  33.         perror("create socket fail.\n");
  34.         exit(0);
  35.     }
  36.    
  37.     struct sockaddr_in server_addr, child_addr;
  38.     bzero(&server_addr, sizeof(server_addr));
  39.     server_addr.sin_family = AF_INET;
  40.     server_addr.sin_port = htons(9998);
  41.     inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
  42.    
  43.     if( connect(s, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 ) {
  44.         perror("connect fail.");
  45.         exit(0);
  46.     }
  47.     str_cli(stdin, s);
  48.     exit(0);
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top