修改后的dg_cli
作者:互联网
void dg_cli(FILE *fp, int sockfd, const struct sockaddr * pservaddr, socklen_t servlen) { int n; char sendline[maxline], recvline[maxline+1]; char errbuff[maxline]; struct sockaddr_in localaddr; socklen_t len=sizeof(localaddr); if(connect(sockfd, (struct sockaddr *)pservaddr, servlen)<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "connect error: %s\n", errbuff); exit(-1); } while(fgets(sendline, maxline, fp) != NULL) { if((n=write(sockfd, sendline, strlen(sendline)))<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "write error: %s\n", errbuff); exit(-1); } if(read(sockfd, recvline, maxline) != n) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "read error: %s\n", errbuff); exit(-1); } if(getsockname(sockfd, (struct sockaddr *)&localaddr, &len)<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "getsockname error: %s\n", errbuff); exit(-1); } recvline[n]=0; fputs(recvline, stdout); printf("local ip: %s, local port: %d\n", inet_ntop(AF_INET, &localaddr.sin_addr, errbuff, sizeof(errbuff)), ntohs(localaddr.sin_port)); } }
标签:sockaddr,socklen,struct,int,dg,char,修改,maxline,cli 来源: https://www.cnblogs.com/donggongdechen/p/16696074.html