#include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <sys/types.h> #include <sys/select.h>
#define SERV_ADDR "127.0.0.1" #define SERV_PORT 5358 #define BUF_LEN 1024
#define MAX(a, b) (a)>(b)?(a):(b)
ssize_t writen(int fd, const void *vptr, size_t n) { size_t nleft; ssize_t nwriten; const char *ptr;
ptr = vptr; nleft = n; while(nleft>0) { if((nwriten = write(fd, ptr, nleft)) <= 0) { if(nwriten < 0 && errno == EINTR) { nwriten = 0; /*interrupt by signal*/ } else { return -1; } }
nleft -= nwriten; ptr += nwriten; } return n; }
void str_cli(FILE *fp, int sockfd) { pid_t pid; char sendbuf[BUF_LEN], recvbuf[BUF_LEN];
if((pid = fork()) ==0) { while(read(sockfd, recvbuf, BUF_LEN) > 0) { fputs(recvbuf, stdout); bzero(recvbuf, BUF_LEN); } kill(getpid(), SIGTERM); exit(0); }
while(fgets(sendbuf, BUF_LEN, fp) != NULL) { writen(sockfd, sendbuf, strlen(sendbuf)); bzero(sendbuf, BUF_LEN); } shutdown(sockfd, SHUT_WR); pause(); return; }
int main(int argc, char **argv) { int fd; struct sockaddr_in servaddr;
fd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(SERV_ADDR); servaddr.sin_port = htons(SERV_PORT);
if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { printf("connect error: %s\n", strerror(errno)); return 0; }
str_cli(stdin, fd); return 0; }
|