bind() 失败:参数无效
bind() Failed: Invalid argument
首先尝试使用套接字程序。任何帮助为什么我会收到此错误?这是我的服务器端代码,直到绑定的部分(不包括监听等)
提前谢谢
int main(){
unsigned int s; //returned by socket()
int port; //port number
int n; //fore read() & write()
int newsockfd; //returned by accept()
struct sockaddr_in server , client;
socklen_t clntLen; // Length of client address data structure
s = socket(AF_UNIX, SOCK_STREAM,0);
if(s==-1){
perror("Socket was not created\n");
return 0;
}
printf("Socket successfully created! ID: %d\n",s);
//sockaddr_in structure
memset( &server, 0, sizeof(server) ); // Zero structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons( 8888 );
//Bind
if( bind(s,(struct sockaddr *)&server , sizeof(server)) < 0){
//print the error message
perror("Bind failed! Error");
return 1;
}
printf("Bind completed successfully\n");
}
在对 socket() 的调用中使用 AF_INET 而不是 AF_UNIX。
首先尝试使用套接字程序。任何帮助为什么我会收到此错误?这是我的服务器端代码,直到绑定的部分(不包括监听等) 提前谢谢
int main(){
unsigned int s; //returned by socket()
int port; //port number
int n; //fore read() & write()
int newsockfd; //returned by accept()
struct sockaddr_in server , client;
socklen_t clntLen; // Length of client address data structure
s = socket(AF_UNIX, SOCK_STREAM,0);
if(s==-1){
perror("Socket was not created\n");
return 0;
}
printf("Socket successfully created! ID: %d\n",s);
//sockaddr_in structure
memset( &server, 0, sizeof(server) ); // Zero structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons( 8888 );
//Bind
if( bind(s,(struct sockaddr *)&server , sizeof(server)) < 0){
//print the error message
perror("Bind failed! Error");
return 1;
}
printf("Bind completed successfully\n");
}
在对 socket() 的调用中使用 AF_INET 而不是 AF_UNIX。