bind()失败:参数无效

时间:2015-11-03 21:59:13

标签: c sockets bind invalid-argument

首先尝试使用socket prog。任何帮助我为什么会收到此错误?这是我的服务器端代码,直到bind的一部分(exlude listen等) thnx提前

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");

}

1 个答案:

答案 0 :(得分:1)

在对socket()的调用中使用AF_INET而不是AF_UNIX。