Sign in to follow this  
Beren77

[java] Chat in Java?

Recommended Posts

Hi, I am currently developing a chess game and I'd love to implement a network feature: Two players can play against each other via "the Internet". So, now my first step was to write a simple chat-application which basically creates a ServerSocket so that a Socket can connect to it. No problem. Now: How does, in a chat application, the client/server actually listen for new messages? Do I really have to implement a polling of messages?? I.e.: "is there a message? No: Sleep for 5 seconds. Yes: Process the message" in an endless loop?? Isn't there a nicer way with sockets? Like a PortListener or something? Or do I have to go for RMI? Will that solve the polling problem? Is that an overkill? (All I want to to in the end is to allow the two players to 1.) chat and 2.) send moves through the same chat window, albeit by making the moves with the mouse instead of typing them, but that doesn't change the nature of the network connection). Any ideas? Hints? Suggestions? How are chats implemented anyway?? You can't tell me, MSN or ICQ keep on polling every x seconds? They cannot, can they?? Thanks! Philipp

Share this post


Link to post
Share on other sites
Hmmm... Thanks for the link, but just now I seem to be too tired to get anything... I should try that later, maybe after a cup of coffee :)

But thanks. Looks good. Non-Blocking seems to be what I am looking for...

Share this post


Link to post
Share on other sites
I've always developed listeners for my network code. Say, WHEN something arrives, do x, as opposed to "Did something arrive so I can do x yet?". That being said, I haven't done this in Java yet, though it's on my short term to-do list. I'd appreciate knowing what you discover.

Share this post


Link to post
Share on other sites
If you want to be "really cool", write a p2p app using JXTA. P2p is perfect for chat and you could also do peer discovery to find game partners using the JXTA rendezvous and discovery protocols.

Share this post


Link to post
Share on other sites
Quote:
Original post by Revelations
I've always developed listeners for my network code. Say, WHEN something arrives, do x, as opposed to "Did something arrive so I can do x yet?". That being said, I haven't done this in Java yet, though it's on my short term to-do list. I'd appreciate knowing what you discover.


java NIO package works event driven.
the selector pop-up from on the keys available (linked to channels) just the ones ready to do soemthing. you get a queue, where "everybody" tells you by their chance: "im ready to write", "im ready to read", or "im ready to connect".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this