This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes how to implement the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how to implement the "session" function in my socket program?" Watch "how to implement the "session" function in my socket program?" New topic
Author

how to implement the "session" function in my socket program?

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
hello
in the j2ee program,i can set the user login info in the session context(such as user name and password or permisson),and trace them during the session,the servlet container help me a lot.
but in my current project,i use the socket communication to connect the c# client and the java server,after the client login in one socket connection,it must close the socket,when it reconnect to the server to send the data,the server don't know who it is,that is the server don't know whether the client login or not and if the client is just one who has just login.
i wonder how can i implement the session function that is similar to servlet do in my socket project?
who can help me?
thank you!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm not exactly sure how the servlet container associates a client to the session. One way is cookies - send a cookie to the client containing a key to a session object in a Map. When the next request comes in, use the cookie to retrieve the session.
You're using raw sockets, so there are no cookies. You could introduce something similar in your request-response protocol. Maybe on the login request the server returns an identity token which the client must send along with all subsequent requests.
I wrote a little web server that can read HTTP headers. Each header is just a line terminated with \n. The end of all headers is a blank line, or \n\n. After that comes the message payload. The logic is roughly:

You get to make up your own headers. You could make one that says

If you find that header, the client already has a session. If not, the message payload must be a login request. If login succeeds, write a "sessionid=xxxx" header in the response. The client might simply echo all headers - making them a place for the server to put state information - or read and interpret headers or create new ones.
This was fun to think about. Let me know if that made sense and how it works out.


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
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks
your idea help me a lot,but i think the xml format message is better,i can put the client id or the status info within some tags.
the other question is if you have some good method to generate the client id?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Google for Java Guid Generator. There will be something cool there. Or start with 1, 2, 3 ... They only have to be unique on one machine and only until the user logs off. BTW: You might want a time-out mechanism so you can periodically purge old sessions or they'll grow forever in memory.
Any chance you will have multiple servers behind a load balancer? Your users might have the IP address of an ArrowPoint or some device like that, and it would divide requests among several real servers. If so, you'll have to find a way to make the user go to the same server after the ArrowPoint makes the first random selection, or a way to copy the session to all servers. Let's hope you can avoid all that!
Oh, XML is fine. I don't introduce XML unless there are some solid benefits. Parsers and DOMs and all that stuff can add complexity; key value pairs work just fine for simple things. Of course if your messages are already XML, then making your "standard header" section fit in is cool.
[ April 10, 2004: Message edited by: Stan James ]
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
thanks for your helps
but where can i get the "Google for Java Guid Generator"?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ahhh, if you don't know Google you're in for a thrill! Point your browser to http://www.Google.com and enter the phrase "Java Guid Generator" (or anything else remotely interesting at the moment) and Google will give you tens of millions of web pages to go read. Ok, maybe less for obscure Java functions. Or maybe more.
People who do this a lot use "google" as a verb to mean "go look it up". If you love it, look into their toolbar and task bar tools to save a little typing.
 
 
subject: how to implement the "session" function in my socket program?
 
Similar Threads
SSH protocol?
How can i Pass String in to Socket .....Urgent..urgent
RMI inside socket
Active Sessions
Multiple sockets cause Threads Hangin