• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your helps
but where can i get the "Google for Java Guid Generator"?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic