Link to home
Start Free TrialLog in
Avatar of brimful
brimful

asked on

Multithreaded server socket authentication

Go morning/afternoon/evening/good night

I am trying to find a way to make sure that every NEW connection needs to go through an authentication process before the server can accept connections from the client. However this should authentication procedure should not affect the clients who are already connected (the server should still continue receiving data from the already authenticated clients). The code I've got is:

import java.net.*;
import java.io.*;
import java.util.*;

/*
 * Class is used to authenticate the user
 */

public class PSWD implements Cloneable, Runnable
{
      Thread runner = null;
      ServerSocket pswd = null;
      Socket client = null;
      BufferedReader br = null;
      PrintWriter pw = null;
      boolean stop = false;
      static boolean authenticated = false;
      
      public synchronized void startPSWD(int port) throws IOException
      {
            if (runner == null)
            {
                  pswd = new ServerSocket(port);
                  runner = new Thread(this);
                  runner.start();
            }
      }

      public synchronized void stopPSWD()
      {
            if (pswd != null)
            {
                  stop = true;
                  runner.interrupt();
                  runner = null;
                  try
                  {
                        pswd.close();
                  }
                  catch (IOException ioe)
                  {
                        System.out.println("PSWD: Error");
                  }
                  pswd = null;
            }
      }

      public void run()
      {
            if (pswd != null)
            {
                  while (!stop)
                  {
                        try
                        {
                              Socket clientSocket = pswd.accept();
                              PSWD newPSWD = (PSWD) clone();

                              newPSWD.pswd = null;
                              newPSWD.client = clientSocket;
                              newPSWD.runner = new Thread(newPSWD);
                              newPSWD.runner.start();
                        }
                        catch (Exception e)
                        {
                        }
                  }
            }
            else
            {
                  run(client);
            }
      }

      public void run(Socket client)
      {
            try
          {
                br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                pw = new PrintWriter(client.getOutputStream(), true);
          }
          catch (IOException ioe)
          {
                  System.out.println("PSWD: Streams failed");
                  System.exit(-1);
          }
}

I'm not too worried about the authentication class at this stage, my primary concern is that if I set an arbitrary boolean value to false I wouldn't affect the clients already connected.

Pseudo code / stubs would be good but example code would be even better.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Because your application is multi-threaded, new connections won't affect already exsting ones. You should allow the Runnable used by the Thread to do the authentication. If it doesn't authenticate, it doesn't run any further. btw, there's no need to clone your whole class as you seem to be doing - all you need is your new Thread and Runnable
> I am trying to find a way to make sure that every NEW connection needs to go through an authentication process

then authenticated should not be static.

you should also have a seperate class for handling client connection.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial