Read about ServerSocket, Socket and threads. Essentially, you create a ServerSocket on your "server" machine, then sit in a loop calling accept(). Every time that returns with a Socket, pass that socket to another thread for processing.
You'll obviously need to invent some "protocol" for how your client makes requests to the server -- what bytes/strings you send for "please log me on as X", "please tell me who's logged on", "please start a chat with Y", "please send this message to Y" etc, and think about how you'll manage e.g. chats from 1 client to multiple people. You'll also need to think about how your client "polls" for messages and/or how you handle the "two directions" of a conversation (you need to send a command to the server at a given moment, but it needs to send you a chat message at a given moment). Two connections can alleviate this, but some firewalls don't allow it.
I assume this is a small project for now. If it was a serious chat system expecting to have large numbers of clients connected, you'd have to think about using something like the Selector class to make it scale (Selector allows thousands of simultaneous connections; with a thread per connection, you'll probably max out somewhere in the low hundreds).
__________________
Neil Coffey
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|