绑定错误 - 套接字编程C.

时间:2013-06-17 11:23:20

标签: c sockets codeblocks

我正在尝试绑定本地套接字,它给了我错误。不确定问题出在哪里。有什么帮助吗?

int result = WSAStartup(MAKEWORD(2,2), &wsadata);
    if(result != NO_ERROR)
    {
      printf("\nThere is a problem at WSAStartup");
    }
    else{
        printf("\nWSAStartup was ok");
    }
    list_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    if(list_sock == -1)
    {
        printf("\n Socket not created %d\n", list_sock);
    }
    else
    {
    printf("\nSocket was created Succesfully");
    }
HOSTENT *thishost;
char *ip;
u_short port;
port = 55555;
thishost = gethostbyname("localhost");
printf("\n");
printf("\nHost name is:%s ",thishost->h_name);
ip = inet_ntoa(*(struct in_addr*)*thishost->h_addr_list);
printf("\nip address is %s\n ", ip);
printf("Address type is:%i",thishost->h_addrtype);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(ip);
service.sin_port = htons(port);
if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))==SOCKET_ERROR)
{
    printf("Error during bind");
}
else
{
        printf("All done");
    }

int result = WSAStartup(MAKEWORD(2,2), &wsadata); if(result != NO_ERROR) { printf("\nThere is a problem at WSAStartup"); } else{ printf("\nWSAStartup was ok"); } list_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); if(list_sock == -1) { printf("\n Socket not created %d\n", list_sock); } else { printf("\nSocket was created Succesfully"); } HOSTENT *thishost; char *ip; u_short port; port = 55555; thishost = gethostbyname("localhost"); printf("\n"); printf("\nHost name is:%s ",thishost->h_name); ip = inet_ntoa(*(struct in_addr*)*thishost->h_addr_list); printf("\nip address is %s\n ", ip); printf("Address type is:%i",thishost->h_addrtype); service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(ip); service.sin_port = htons(port); if(bind(list_sock,(SOCKADDR *)&service,sizeof(service))==SOCKET_ERROR) { printf("Error during bind"); } else { printf("All done"); } 它创建了套接字,我可以显示IP地址,名称等。但是当绑定发生时它会给我错误。

此致 小号

1 个答案:

答案 0 :(得分:0)

如果在第二次运行程序时发生,请检查SO REUSE_ADDRESS。