0

By using select function handling multiple client connection in RPI using c. If CLOSE_WAIT came code is not working.if it occurs I am trying to kill the socket but code is hanging. Without restart how to resolve this Please help in this.....

When CLOSE_WAIT will come how to avoid this.

I am thinking it will come if we didn't close the socket but how to close during network loss. Or else How to do read timeout on server side

6
  • "If CLOSE_WAIT came code is not working" - then you are doing something wrong. Please show your actual code. CLOSE_WAIT happens on the server side only when a client gracefully closes its end of the connection (ie sends a FIN packet) before the server closes its end. Once the server has closes its end, the connection transitions out of CLOSE_WAIT. A connection being in CLOSE_WAIT will not prevent the server from handling other connections. It sounds like your server is simply not closing its sockets properly, such as if it is not handling socket close/error notifications correctly. May 16, 2020 at 18:18
  • So, you just copy/pasted that code as-and compiled it is? Because that code has bugs in it. For instance, it is not close()'ing accepted sockets if the clients[] array is full. And it is handling errors from send() and read() at all. But it is handling graceful disconnects, at least, and should be close()'ing sockets for disconnected clients. But it is not close()'ing sockets for lost clients. May 16, 2020 at 20:35
  • Yes I am using this as it is but actual my project is based on the client input RPI want to switch from AP to client when switching between the network I am closing the socket setting that array to zero and if array size cross 10 I am closing all opened socket. But before this i am getting CLOSE_WAIT and code is hanging
    – kumuda.m
    May 17, 2020 at 2:14
  • i don't think you understand what CLOSE_WAIT actually is. The code shown can't hang if CLOSE_WAIT occurs. Something else is going on. Like maybe no new client connecting to your server. Use a timeout with select(). Also, select() isn't the best choice for error handling. epoll() is a better option. May 17, 2020 at 2:18

1 Answer 1

0

Call shutdown(fd, 2) before close(fd), otherwise your socket will go into CLOSE_WAIT state to wait for the FIN handshake. This can take quite a while. I believe it is four minutes on Solaris, for example.

If you have a newer unix, you can use SHUT_RDWR instead of 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.