socket_client.c

Go to the documentation of this file.
00001 #include <string.h>
00002 #include <sys/types.h>
00003 #include <sys/socket.h>
00004 #include <arpa/inet.h>
00005 
00006 #include "proxylib.h"
00007 #include "echo.h"
00008 
00009 int ns_errno;
00010 
00011 void str_cli(FILE *, int);
00012 
00013 int main(int argc, char **argv)
00014 {
00015   int sockfd;
00016   struct sockaddr_in servaddr;
00017 
00018   if (argc != 3) {
00019     fprintf(stderr, "usage: tcpcli <IPaddress> <port>");
00020     exit(-1);
00021   }
00022 
00023   sockfd = socket(AF_INET, SOCK_STREAM, 0);
00024 
00025   memset(&servaddr, 0x0, sizeof(servaddr));
00026   servaddr.sin_family = AF_INET;
00027   servaddr.sin_port = htons(atoi(argv[2]));
00028   inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
00029 
00030   connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
00031 
00032   str_cli(stdin, sockfd);   /* do it all */
00033 
00034   exit(0);
00035 }
00036 
00037 void str_cli(FILE * fp, int sockfd)
00038 {
00039   char sendline[MAXLINE], recvline[MAXLINE];
00040 
00041   while (fgets(sendline, MAXLINE, fp) != NULL) {
00042 
00043     Writen(sockfd, sendline, strlen(sendline));
00044 
00045     if (Readline(sockfd, recvline, MAXLINE) == 0) {
00046       fprintf(stderr, "str_cli: server terminated prematurely");
00047       return;
00048     }
00049 
00050     fputs(recvline, stdout);
00051   }
00052 }