学习套接字编程, 在客户端程序出错, 输入如下
$ ./client 127.0.0.1
服务端程序已经运行,且有端口 9877 LISTEN
运行的时候connect出错,出错代码errno为88
客户端代码如下
#include "apue.h"void str_cli(FILE *fp, int sockfd)
{
char sendline[MAXLINE], recvline[MAXLINE];
while(fgets(sendline, MAXLINE, fp) != NULL)
{
if(write(sockfd, sendline, strlen(sendline)) == -1)
printf("write error.\n");
readline(sockfd, recvline, MAXLINE);
if(fputs(recvline, stdout) < 0)
printf("\n fputs error.\n");
}
if(ferror(fp) != 0)
printf("fgets error.\n");
}int main(int argc, int *argv[])
{
int sockfd;
struct sockaddr_in servaddr; if(argc != 2)
error_out("usage:./a.out ");
if(sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
error_out("socket error.");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) < 0)
error_out("inet_pton error.");
if(connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
error_out("connect error.");
printf("connect after errno = %d\n", errno);
str_cli(stdin, sockfd);
exit(0);
}