Meaningless Drivel is fun!*
The moose likes Sockets and Internet Protocols and the fly likes Sending Username/Password over Socket Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending Username/Password over Socket Connection" Watch "Sending Username/Password over Socket Connection" New topic
Author

Sending Username/Password over Socket Connection

Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
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.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

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.


[How To Ask Questions On JavaRanch]
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
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...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Originally posted by Timothy Frey:
the Sun Socket tutorial is great at explaining sockets but doesn't even touch upon custom protocols (that I can see, anyway).


The protocol example is in the section Writing the Server Side of a Socket and is encapsulated in the KnockKnockProtocol class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending Username/Password over Socket Connection