I'm writing an AIM clone and so far everything went pretty smoothly. Instead of automatically letting the client connect to the server and letting them join the chat session, I'd like to make sure that they are registered with the server. I've already implemented a fairly flexible design for storing user information but the problem I'm having is how to get the Client to tell the server what user is trying to sign in.
Right now the Server basically has a single thread for each Client that has connected and awaits input from the socket connection. What would be the proper way to send the Client's information to the Server after the client has connected but before they have joined the chat session? There's probably an easy solution but so far the only thing I've done with sockets is to send plain text back and forth.
You need to think about a protocol. Something that lets you differentiate commands (login) from data ("joe" or "I'm fine, how are you"). The Java Tutorial has a simple example in the custom networking trail. One simple alternative is to use object serialization to do the IO and create different classes for commands and data. Another alternative is to dispose with protocols and use remote method invocation to invoke different commands on the server.
I figured it had something to do with writing a custom protocol. Unfortunately, the Sun Socket tutorial is great at explaining sockets but doesn't even touch upon custom protocols (that I can see, anyway). I plan on using RMI in my next project but for now I'd like to stick with plain ol' sockets.
Does anyone have an idea where I can find resources on writing custom protocols? I've been looking but haven't really found anything of use...
A custom protocol can be as simple or complex as you like. If you read through the HTTP spec you'll find there is nothing magical. Headers mostly look like "name=value", each header is terminated with newline, a blank line marks the end of headers, data follows that. Yours might be HTTP-like or just a fixed-length code that says what kind of message this is or a set of XML or JSON structures.
Think about how many different message type you'll need to send, what it would take to build and parse messages, what return codes or messages you need to send back. If you just write them all down you might find you've invented a protocol.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi