Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multithreaded server socket authentication

Posted on 2004-04-09
4
Medium Priority
?
2,046 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:brimful
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10793509
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
0
 
LVL 92

Expert Comment

by:objects
ID: 10794814
> 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.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10794823
So it would look something like:


public class PSWD implements Cloneable, Runnable
{
     Thread runner = null;
     ServerSocket pswd = null;
     boolean stop = 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();
                         PSWDClient newPSWD = new PSWDClient(clientSocket);
                    }
                    catch (Exception e)
                    {
                    }
               }
          }
     }
}

public class PSWDClient implements Runnable
{
     Thread runner = null;
     Socket client = null;
     BufferedReader br = null;
     PrintWriter pw = null;
     boolean stop = false;
     boolean authenticated = false;

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

         public void run()
         {
             // handle client comms
         }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 10799122
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question