[Chapter11] 由于没加括号导致的bug——调了一个多小时
作者:互联网
在本章写远程ls程序rls.c和rlsd.c时,while( msglen = read( sock_id, message, BUFSIZ ) > 0 )导致了严重的错误——客户端收到服务器返回的信息,但什么都不打印。
具体代码如下:
/* rls.c - client program of remote ls * ./rls ubuntu 15000 'path' */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> int main( int argc, char *argv[] ) /* * 1.get a socket * 2.set server addr * 3.connect server * 4.write path to socket stream * 5.get message from server */ { struct sockaddr_in saddr; struct hostent *hp; int sock_id; char message[BUFSIZ]; int msglen; sock_id = socket( AF_INET, SOCK_STREAM, 0 ); bzero( (void *)&saddr, sizeof(saddr) ); hp = gethostbyname( argv[1] ); bcopy( hp->h_addr, (void *)&saddr.sin_addr, hp->h_length ); saddr.sin_port = htons( atoi(argv[2]) ); saddr.sin_family = AF_INET; connect( sock_id, (struct sockaddr *)&saddr, sizeof(saddr) ); write( sock_id, argv[3], strlen(argv[3]) ); write( sock_id, "\n", 1 ); while( (msglen = read(sock_id, message, BUFSIZ)) > 0 ) write( 1, message, msglen ); close( sock_id ); }
标签:没加,sock,argv,bug,Chapter11,saddr,message,include,id 来源: https://www.cnblogs.com/uestcliming666/p/13140154.html