Register clients to server - java sockets programming

new_learner

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 ?

Kumar Gaurav

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Sockets: One Server and Multiple Clients

From Dev

Java sockets - How to simulate multiple clients

From Dev

Java Sockets - Messages between many clients

From Dev

Java sockets, echo server

From Dev

Server with Multiple Clients - JAVA

From Dev

Sockets Programming with Python: Get the port of a server

From Dev

Sockets Programming with Python: Get the port of a server

From Dev

C++ Sockets - Server doesn't accept multiple clients (linux)

From Dev

I need the server to send messages to all clients (Python, sockets)

From Dev

How to send data from Server to multiple Clients using Sockets?

From Dev

Java Sockets - Sending Objects and distributing the object to all clients that are connected

From Dev

Best approach for large file transfer to multiple clients using java sockets

From Dev

Networking with ObjectInputStream, Java and Lua clients across TCP sockets

From Dev

Sending ByteArray using Java Sockets (Android programming)

From Dev

Java sockets: updating on client but not server?

From Dev

How to register server on Consul [Java]

From Dev

Server cannot receive the clients message in c winsock programming

From Dev

Server(Python) - Client(Java) communication using sockets

From Dev

ObjectStreams with sockets hang at server and client side java

From Dev

how to use java sockets to connect to an online server

From Dev

Java Sockets sending multiple objects to the same server

From Dev

Java Sockets - send data from server to client

From Dev

Server(Python) - Client(Java) communication using sockets

From Dev

Java Sockets - Sending data from client to server

From Dev

Multiple UDP sockets and multiple clients

From Dev

How to register eureka-clients with eureka-server on different hosts. Spring-boot

From Dev

How to register eureka-clients with eureka-server on different hosts. Spring-boot

From Dev

C - sockets - epoll. What with slow clients?

From Dev

Return message to Android client from java Server via tcp sockets

Related Related

  1. 1

    Java Sockets: One Server and Multiple Clients

  2. 2

    Java sockets - How to simulate multiple clients

  3. 3

    Java Sockets - Messages between many clients

  4. 4

    Java sockets, echo server

  5. 5

    Server with Multiple Clients - JAVA

  6. 6

    Sockets Programming with Python: Get the port of a server

  7. 7

    Sockets Programming with Python: Get the port of a server

  8. 8

    C++ Sockets - Server doesn't accept multiple clients (linux)

  9. 9

    I need the server to send messages to all clients (Python, sockets)

  10. 10

    How to send data from Server to multiple Clients using Sockets?

  11. 11

    Java Sockets - Sending Objects and distributing the object to all clients that are connected

  12. 12

    Best approach for large file transfer to multiple clients using java sockets

  13. 13

    Networking with ObjectInputStream, Java and Lua clients across TCP sockets

  14. 14

    Sending ByteArray using Java Sockets (Android programming)

  15. 15

    Java sockets: updating on client but not server?

  16. 16

    How to register server on Consul [Java]

  17. 17

    Server cannot receive the clients message in c winsock programming

  18. 18

    Server(Python) - Client(Java) communication using sockets

  19. 19

    ObjectStreams with sockets hang at server and client side java

  20. 20

    how to use java sockets to connect to an online server

  21. 21

    Java Sockets sending multiple objects to the same server

  22. 22

    Java Sockets - send data from server to client

  23. 23

    Server(Python) - Client(Java) communication using sockets

  24. 24

    Java Sockets - Sending data from client to server

  25. 25

    Multiple UDP sockets and multiple clients

  26. 26

    How to register eureka-clients with eureka-server on different hosts. Spring-boot

  27. 27

    How to register eureka-clients with eureka-server on different hosts. Spring-boot

  28. 28

    C - sockets - epoll. What with slow clients?

  29. 29

    Return message to Android client from java Server via tcp sockets

HotTag

Archive