Sockets bug?
Colin (478) 2433 posts |
Is this a Sockets bug? SockTest.zip is a socket demonstration. There are 2 programs client and server you run server in one taskwindow and client in another taskwindow – see the !readme file. When both are running what you type in either window is echoed in the other – the taskwindows can be on the same or different machines. The problem is if I press escape in the server window the client window is correctly notified via socketread returning a size of 0 and stops. But if I escape in the client window the server window is not notified ie socketread doesn’t return 0 and so the server is not informed of the client closing. Both client and server use the same code to read from the socket (terminal.c). I don’t see why it matters which window I press escape in the other should be notified shouldn’t it? Any thoughts? |
Rick Murray (539) 13872 posts |
I’ve not looked at your code (sitting in the garden enjoying the sun), but from memory the code to read data does not return whether or not the socket is connected – it can validly return zero bytes available. For my server I used a different call (recv?) to see if the socket is still connected prior to socketread to actually read data. |
Colin (478) 2433 posts |
socketread is supposed to return 0 to indicate EOF when the remote socket closes – which is what happens if escape is pressed in the server window in my demo (the client window closes its socket because it receives the read size 0). I’m trying to figure out why it works if I escape the server but not when I escape the client. I can only think its a bug. |
Rick Murray (539) 13872 posts |
That’s what the ROLtd documentation says… How, pray-tell, does the Therefore, the code that I use is as follows:
As I said earlier in the week, sockets aren’t particularly hard, just quirky and mildly annoying. ;-) [I’ll await a metric tonne of people saying “argh! no! you can’t do that!”] |
Colin (478) 2433 posts |
Thank goodness for that. It was a bug in my program. Line 42 of client.c should be
and not
sock is a global so that it is closed atexit so it now works properly – pressing escape in either window closes the program in the other window. I’ve fixed the download if anyone wants to see how to write the simplest C program demostrating how to write a client and server I can think of. Rick It returns EWOULDBLOCK. FTPc uses this and the fact that a read of size = 0 means the other end has been closed and has never had problems. terminal.c shows in the download how I always read. |
Rick Murray (539) 13872 posts |
Strange. I’ll need to look at my code again, as that isn’t what I recall seeing. |