-
July 16th, 2023, 02:31 AM
#1
return value of recv when process is killed
In network where process C is client and another one S is server,
C is waiting network message or user input by select function, idem S.
If I kill process A, RECV of server S receives -1 value while I'd be to wait 0 (closed socket).
The behaviour is the same when I killed process by click ctrl-C keys sequence.
My target is in Server (old std C89), where I want to detect remote closed socket so that I can close bad local socket.
How can I distinct this action if result value is always -1 ?
-
July 16th, 2023, 03:21 AM
#2
Re: return value of recv when process is killed
-
July 17th, 2023, 09:05 AM
#3
Re: return value of recv when process is killed
I noted select return 1 when remote process is receiving ctrl-c while I thought it received -1 value.
I create server process where stdin and communication sockets are inserted into fd_set to monitor, same thing for client process.
In both cases if I kill one process of the two processes , select return 1 and I found out ready process to read is the communication socket.
So select doesn't know info about remote socket is closed but I gave it by recv function which will return 0.
Right ?
My doubt is because socket is always ready? I know I have to remove this socket to monitor but I thought if recv is executed there is no reason to see it as ready socket from select function.
Last edited by zio_mangrovia; July 17th, 2023 at 09:10 AM.
-
July 17th, 2023, 11:06 PM
#4
Re: return value of recv when process is killed
> I noted select return 1 when remote process is receiving ctrl-c while I thought it received -1 value.
Select() only tells you that something happened on a monitored fd.
You then need to call recv() (or whatever is appropriate for that fd) to find out what actually happened.
So for example, if recv() returned 0, the remote closed the connection and there will be no more data.
Meaning you should also close(), and remove the fd from the set of fd's being monitored by select()
If recv() returned -1, it's an error. You look at errno and decide whether to give up or try to carry on.
For example, EAGAIN and EINTR are usually worth a retry. Most everything else is permanently fatal.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|