Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register clients to server - java sockets programming

I am new to socket programming. I am developing a multiple client -server system. In that the clients need to register their credentials when they are sending connection request to the server. The server will authenticate it.

I have read some tutorials on client server programming. I understood how the sockets work. But my question is what should I do for this registration and authentication? Can I just send a username pwd as message in the output stream? Or is there any other better way to achieve this ?

Please guide me how do I achieve this ?

like image 215
new_learner Avatar asked Dec 10 '25 00:12

new_learner


1 Answers

You can do this in a similar manner as web server do, by maintaining a session. Here goes the steps.

  1. On First Client Time connection client sends a credential pair (username and password)
  2. Server accept this and authenticate, if authenticated it create a hashcode out of it (username & password) and add into list of active session. and return the hashed session to client
  3. On 2nd request or later on client sends the hashed session and server check that session in active list of session.

Client Code:

Class Client
{
    private String uname;
    private String password;
    private String session;
    public String[2] message;
    //getter setter
}

//Inside Main Class
int sendInfo(String mesg)
{
    Client c=getClient(); //this function gives your Client Object with either session and username password etc preset,
    if(c.getSession()!==null || !c.getSession()!="")
    {
        c.message[0]=c.getSession(); //first String contains your session 
        c.message[1]=mesg;          //next string contains your message
    }
    else
    {
        c.message[0]=c.getUsername+"|"+c.getPassword(); //first String contains your session 
        c.message[1]=mesg;          //next string contains your message
    }
    sendMessageToServer(c.message);
}

Server Code

Class Server
{
    private List<String> activeSession();
    //getter setter
    void addActiveSession(String session)
    {
        this.activeSession.add(session);
    }
    String generateSession(String uname,String password)
    {
        String sessionHash="RemoteSession"+this.activeSession.size()+uname+password;
        return sessionHash;
    }
    void serverReceive()
    {
        //Logic for listener
        String msg[]=getClientMessage();
        if(msg[0].indexOf('|')<0)
        {
            //it contains session. Check in activeSesion list           
        }
        else
        {
        //It cotains username & password separeted by |. Call Authenticate
            String 
        }
    }
    void authenticateConnection(String username,String pass)
    {
        if(checkUnameandPassword(username,pass))
        {
            //if the uname and pass is valid
            this.addActiveSession(generateSession(username,password))
        }
        else
        {
                sendConnectionrefuse();
        }
    }
}

Hope this will help you

like image 101
Kumar Gaurav Avatar answered Dec 12 '25 12:12

Kumar Gaurav