linux socket accept无法阻止

时间:2013-03-16 13:14:29

标签: linux sockets connection block

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <sys/socket.h>

    int main()
    {
    int server_sockfd,client_sockfd;
    int server_len,client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

创建一个新套接字

    unlink("server_socket");
    server_sockfd=socket(AF_INET,SOCK_STREAM,0);

将其命名为

    server_address.sin_family=AF_INET;
    server_address.sin_addr.s_addr=htonl(INADDR_ANY);
    server_address.sin_port=htons(9734);
    server_len=sizeof(server_address);
    bind(server_sockfd,(struct sockaddr *)&server_address,server_len);

设置块

    int flags=fcntl(server_sockfd,F_GETFL,0);
    if(flags&O_NONBLOCK==1){
       printf("NONBLOCK");
    }else{
       printf("BLOCK");
    }
    flags=flags&~O_NONBLOCK;
    fcntl(server_sockfd,F_SETFL,flags);
    if(flags&O_NONBLOCK==1){
       printf("NONBLOCK");
    }else{
       printf("BLOCK");
    }

    listen(server_sockfd,5);
    while(1){
       char ch;
       printf("server waiting\n");
       client_len=sizeof(client_address);
       client_sockfd=
          accept(server_sockfd,(struct sockaddr*)&client_sockfd,&client_len);

第一次被阻止

      read(client_sockfd,&ch,1);
      ch++;
      write(client_sockfd,&ch,1);
      close(client_sockfd);
   }
}

当客户端首先连接时,我工作,但下一个将无法正常工作

1 个答案:

答案 0 :(得分:1)

client_addressclient_sockfd之间可能会出现不匹配。

man page of accept()说:

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

The addrlen argument is a value-result argument: the caller must initialize it to
contain the size (in bytes) of the structure pointed to by addr; on return it
will contain the actual size of the peer address.

尝试:

client_sockfd=
   accept( server_sockfd, (struct sockaddr*)&client_address, &client_len );