Skip to main content
gamedev.net
🔒 Locked

[Java] Multi-Socket Question

Started by Chryzmo 3 replies 1.7k views
Original Post
Chryzmo
Chryzmo
Hey all, I am looking at writing a multi-socket server in Java and I've been looking how to go about designing it. From what I've read it seems that Java doesn't have a select() type function like the C socket library. This would seem, to me, to mean that to get the same functionality from a Java application you would need to create a new thread for each socket that connects and then write a Manager class to check when each socket has data that is ready to be utilized. I was hoping someone could help me out as to whether I'm on the right track with this. Thanks, Chryzmo
Antheus
Antheus
Quote:
Original post by Chryzmo

I am looking at writing a multi-socket server in Java and I've been looking how to go about designing it. From what I've read it seems that Java doesn't have a select() type function like the C socket library.


No it doesn't. Sockets are blocking.

Quote:
This would seem, to me, to mean that to get the same functionality from a Java application you would need to create a new thread for each socket that connects and then write a Manager class to check when each socket has data that is ready to be utilized.


When you accept a new socket, you spawn a thread, and then do a blocking read in an infinite loop for that particular socket.

Quote:
I was hoping someone could help me out as to whether I'm on the right track with this.


If you intend to support a dozen or so clients, then it'll work.

For any reasonable socket server however, you'll need to look into NIO, which allows for select-like approach.
Kaze
Kaze
I just had thread A have a blocking ServerSocket that listens for connections and then dumps the created sockets into a queue and thread B (or thread pool B) writes and reads from them without blocking by only reading available bytes, this setup wasn't under a ton of load but worked fine for me.
Aldacron
Aldacron
Quote:
Original post by Chryzmo
I am looking at writing a multi-socket server in Java and I've been looking how to go about designing it. From what I've read it seems that Java doesn't have a select() type function like the C socket library.


Java has had select() functionality, and non-blocking sockets, since 1.4 in the java.nio package. Google Java NIO for tutorials, articles, books and such.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.