struct args { /* 存储参数的结构 */ long arg1; long arg2; }; struct result { /* 存储结果的结构 */ long sum; };
void str_cli(FILE *fp, int sockfd) { char sendline[MAXLINE]; struct args args; struct result result; while(fgets(sendline, MAXLINE, fp) != NULL) { if (sscanf(sendline, "%ld%ld", &args.arg1, &args.arg2) != 2) { printf("invalid input: %s\n", sendline); continue; } writen(sockfd, &args, strlen(args)); if (read(sockfd, recvline, MAXLINE) == 0) { return; } printf("%ld\n", result.sum); } }
void str_echo(int sockfd) { ssize_t n; struct args args; struct result result; for ( ; ; ) { if ( (n = read(sockfd, &args, sizeof(args)) == 0) { return; } result.sum = args.arg1 + args.arg2; writen(sockfd, &result, sizeof(result)); } }
原文:https://www.cnblogs.com/soldierback/p/10696729.html