Socket operation on non-socket 错误出现有两种情况:
1. 建立socket:
if(listenfd= socket(AF_INET,SOCK_STREAM, 0)==-1){
perror("creating socket failed!");
exit(-1);
}
会造成在bind时出现 Socket operation on non-socket错误
正确的代码应该是:
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("creating socket failed!");
exit(-1);
}
2. accept时:
if(connfd=accept(listenfd,(struct sockaddr *)&client_addr,(socklen_t *)&sin_size)==-1){
perror("accept error!");
exit(-1);
}
会造成在recv时出现 Socket operation on non-socket错误
正确代码是:
if((connfd=accept(listenfd,(struct sockaddr *)&client_addr,(socklen_t *)&sin_size))==-1){
perror("accept error!");
exit(-1);
}
出现Socket operation on non-socket 错误的原因是:
if(listenfd= socket(AF_INET,SOCK_STREAM, 0)==-1)
if(connfd=accept(listenfd,(struct sockaddr *)&client_addr,(socklen_t *)&sin_size)==-1)
这两句中缺失了()造成的。赋值符合优先级最低,导致listenfd和connfd在创建/连接成功是为0,不成功时为1