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?
Do something like cookies.
When the client first connects to the server, have the server send it a session id. The client should store the id in memory.
When the client reconnects, the first value it should send is the session id, which the server uses to correlate the new connection will older conversational state.
Alternately, use an HTTP client talking to a servlet instead of a generic socket, and use the cookie/sessionId mechanisms of servlets.
and what's better method to generate the session id do you think?
It does not matter, so long as it is unique. A timestamp (System time in millis) plus a random long integer (generated by an incrementing counter) ought to work. Heck, the auto-incremented counter ought to work by itself, but I am a bit paranoid when it comes to unique ids.