Forum Navigation
Line Delimited Network IO - "Recommended Approach"
Posted: 12/29/08 22:19:43 Modified: 12/29/08 22:27:18I'm trying to write an app that needs to read lines of text from network sockets in a non-blocking manner, and I'm not sure how I should read them.
In Java, I would just have a thread for each socket that would make a blocking call (to the equivalent of LineInput?.readln) to get a line. This worked because in Java, closing a socket caused all threads making use of the socket to return failure, so I could stop my "listener threads" fairly simply. I'm not sure if this approach would work in D, although even if it didn't, I could make it work by setting all "listener threads" to be daemon threads. While this would waste resources, the program would function normally.
In C and C++, I would use normal network functions and store incoming characters to a buffer, searching for line endings. This approach was more that a little inefficient and unsafe, but it worked. It could be made to work in D, but it seems like there should be a better way.