#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#define MAXLINE 4096
void str_cli(FILE *fp, int sockfd) {
int n;
char sendline[MAXLINE+1], recvline[MAXLINE+1];
if((fp = fopen("/root/iplist", "r")) == NULL) {
printf("fopen error");
exit(-1);
}
while(fgets(sendline, MAXLINE, fp) != NULL) {
if((write(sockfd, sendline, strlen(sendline))) != strlen(sendline)) {
printf("write error\n");
exit(-1);
}
if((n=read(sockfd, recvline, MAXLINE)) < 0) {
printf("read error\n");
exit(-1);
}
recvline[n]=0;
if(fputs(recvline, stdout) == EOF) {
printf("fputs error\n");
exit(-1);
}
}
if(fclose(fp) == EOF) {
printf("fclose error\n");
exit(-1);
}
}
int main(int argc, char **argv) {
int sockfd;
struct sockaddr_in servaddr;
FILE *fp;
if(argc != 2) {
printf("usage: a.out <IPaddress>\n");
exit(-1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("socket error");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8000);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("inet_pton error");
exit(-1);
}
if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
printf("connect error");
exit(-1);
}
str_cli(fp, sockfd);
exit(0);
}
客户端打开本地文件,发送给服务器端,然后服务器端再把内容回射回来。