Non blocking sockets

Watched quite a heavy film last night so didn’t start the day with bells and whistles. Anyway, back on with Java, today’s task really was just to complete my conversion of my server code from C++. Spent the bulk of the day on the server side TCP handling. I’m not going for SSL straight away as that’s just going to complicate things. Now the usual way of doing this is to either create a number of threads and then each one of them listens on a socket and then creates a client or to have a main thread that listens and then create threads as new connections come in. Talking to the guys it appears there is another way to do it in Java. It uses a ‘Selector’, so what you do is set up a socket stream and then set that no non-blocking. You then create a selector and add the socket to it and tell it you want it to monitor ‘accept’. Then you just sit in a loop iterating over this selector array. On accepts you just create the client socket and say you want to monitor read and write. Reads are fairly straight forward, writes I’m not so sure about yet as normally output shouldn’t be blocked, but I’m thinking if writes are suddenly unavailable then may be this is good way of checking for disconnections. Anyway, got it all as far as running and accepting a connection and then crashing, but that’s a job for another day. Not sure how much more time I’ll be allowed to spend on it, but if I’m given a couple more days then I think I’ve pretty much got the new language mastered.

Other activities, well not a great deal. Walked the dog as it’s my turn on Friday’s. She was actually really good. I took some biscuits with me and rewarding her when she didn’t try and declare war on other dogs. Went to the gym and did pump. Now I think it’s time to shutdown, both the machine and myself.

Leave a Reply

Your email address will not be published.