我的代码是:

int main(int argc, char *argv[])
{
    int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
    struct sockaddr_in my_addr;    /* my address information */
    struct sockaddr_in their_addr; /* connector's address information */
    socklen_t sin_size;

    /* generate the socket */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    /* generate the end point */
    my_addr.sin_family = AF_INET;         /* host byte order */
    my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
    my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
        /* bzero(&(my_addr.sin_zero), 8);   ZJL*/     /* zero the rest of the struct */

    /* bind the socket to the end point */
    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
    == -1) {
        perror("bind");
        exit(1);
    }

    /* start listnening */
    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    printf("server starts listnening %d...\n",sockfd);

    /* repeat: accept, send, close the connection */
    /* for every accepted connection, use a sepetate process or thread to serve it */
while(1) {  /* main accept() loop */
    sin_size = sizeof(struct sockaddr_in);
    if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
    &sin_size)) == -1) {
        perror("accept");
        continue;
    }
    printf("server: got connection from %s\n", \
        inet_ntoa(their_addr.sin_addr));

    if ((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1) {
    perror("recv");
    exit(1);
    }

    buf[numbytes] = '\0';

    printf("Received: %s",buf);

    if (send(new_fd, "Hello, world!\n", MAXDATASIZE, 0) == -1)
        perror("send");
        close(new_fd);
        exit(0);

    close(new_fd);  /* parent doesn't need this */

    while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
return 0;
}

因此,每当我执行此服务器时,一个客户端使用它后,它将终止。但是,如果我想在60秒内再次执行它,那么它给出了bind: Address already in use错误,我认为close()函数实际上释放了套接字,因此可以立即再次使用它。那我在这里想念什么?

最佳答案

在调用bind之前,您可以使用SO_REUSEADDR套接字选项标记要潜在地重用地址/端口:

int reuseaddr = 1;
int err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
                     &reuseaddr, sizeof(reuseaddr));

关于c - 在BSD插槽中关闭插槽,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13041335/

10-15 00:29