Socket with recv-timeout: What is wrong with this code?

前端 未结 6 2269
难免孤独
难免孤独 2021-02-20 16:41

I\'m trying to implement a socket with a recv timeout of 1 Second:

int sockfd;
struct sockaddr_in self;
struct sockaddr_in client_addr;
int addrlen=sizeof(clien         


        
6条回答
  •  时光说笑
    2021-02-20 17:20

    Try using select() before calling recv() or accept().

    select() takes an array of file descriptors (includinig sockets) and returns when at least one of them is ready to receive. It can also return on a timeout.

    In linux you can also try poll() (not sure if Winsock provides this).

提交回复
热议问题