Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:33:32+00:00 2026-06-02T23:33:32+00:00

Im trying to make a basic email system in Java. I have a server

  • 0

Im trying to make a basic email system in Java. I have a server that the clients connect to and a gui for the clients. When the server picks up a new connection from a client it starts a new thread running that handles all the different operations. The problem is that the server accepts the new client but doesnt start a new client handler thread running. Any help?

public class Server {
   public static Connection link;

   public static void main(String[] args) throws IOException {
      ServerSocket serverSocket = null;
      final int PORT = 1234; // Define the sockets and ports and i/o
      Socket client;
      ClientHandler handler;

      try {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         link = DriverManager.getConnection("jdbc:odbc:emails", "", "");
         System.out.println("Connected to Database . . . ");
      } catch (ClassNotFoundException cnfEx) {
         System.out.println("* Unable to load driver! *");
         System.exit(1);
      } catch (SQLException sqlEx) {
         System.out.println("* Cannot connect to database! *");
         System.exit(1);
      }

      try {
         serverSocket = new ServerSocket(PORT); // Set the server socket
      } catch (IOException ioEx) {
         System.out.println("\nUnable to set up port!"); // If failed let the
                                                         // user know
         System.exit(1); // Exit the system with error code 1
      }

      System.out.println("\nServer running...\n"); // Tell the user the server
                                                   // is running

      do {
         client = serverSocket.accept();
         // Wait for client.
         System.out.println(client);
         handler = new ClientHandler(client);
         System.out.println(handler);
         System.out.println("\nNew client accepted.\n"); // Tell the user the
                                                         // server has accepted
                                                         // a client
         System.out.println("RUNNING");
         handler.start(); // Start the handler

      } while (true); // Continuous loop
   }

   static class ClientHandler extends Thread {
      /**
 * 
 */
      private Socket client;
      private ObjectInputStream input; // Define sockets, i/o and local array
                                       // list
      private ObjectOutputStream output;

      public ClientHandler(Socket socket) throws IOException // Client handler
                                                             // constructor
      {
         client = socket;
         input = new ObjectInputStream(socket.getInputStream());// Set client
                                                                // and i/o
                                                                // stream
         output = new ObjectOutputStream(socket.getOutputStream());
      }

      public void run() {
         String userCommand = null;
         System.out.println("RUNNING CLIENT HANDLER");
         boolean quit = false;

         do {

            try {
               userCommand = (String) input.readObject();
            } catch (IOException e2) {
               // TODO Auto-generated catch block
               e2.printStackTrace();
            } catch (ClassNotFoundException e2) {
               // TODO Auto-generated catch block
               e2.printStackTrace();
            } // Receive user command

            if (userCommand.equals("SEND MESSAGE")) // If user command is to
                                                    // send a message
            {
               String username = null, recipient = null, message = null, insert, fileType = null;

               try {
                  username = (String) input.readObject();
                  recipient = (String) input.readObject(); // Receive username,
                                                           // recipient and
                                                           // message
                  message = (String) input.readObject();
                  fileType = (String) input.readObject();
               } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } catch (ClassNotFoundException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               }

               insert = "INSERT INTO [emails] ([From], [To], [Message], [Attachment])"
                     + " VALUES('"
                     + username
                     + "','"
                     + recipient
                     + "','"
                     + message + "','" + fileType + "')";
               Statement statement = null;
               try {
                  statement = link.createStatement();
                  statement.executeUpdate(insert);
                  link.commit();
               } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }

               try {

                  getFile(input, fileType);
               } catch (IOException ioEx) {
                  ioEx.printStackTrace();
               } catch (ClassNotFoundException cnfEx) {
                  cnfEx.printStackTrace();
               }

               System.out.println(username // Tell the user a message has been
                                           // sent
                     + " sent message to " + recipient);
            }

            if (userCommand.equals("READ MESSAGE")) // If user command is to
                                                    // read a message
            {
               String username = null; // Define local variables

               try {
                  username = (String) input.readObject();
               } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } catch (ClassNotFoundException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } // Receive the username of the current user
               Statement statement = null;
               String insert = "SELECT [Email No],[From],[Message],[Attachment] FROM [emails] WHERE To = '"
                     + username + "'";
               ResultSet rs = null;
               try {
                  statement = link.createStatement();
                  rs = statement.executeQuery(insert);
               } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }

               try {
                  while (rs.next()) {
                     String from = rs.getString("From");
                     String message = rs.getString("Message");
                     String attachment = rs.getString("Attachment");
                     int emailNo = rs.getInt("Email no");
                     output.writeObject(from);
                     output.writeObject(message); // Send the username the
                                                  // message is from
                     output.writeObject(attachment);
                     output.writeObject(emailNo);
                     output.flush(); // and the message and flush the output
                                     // stream
                  }
               } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }

               try {
                  output.writeObject("END");
               } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               } // Send an end message
               try {
                  output.flush();
               } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               } // Flush the output stream

               System.out.println(username // Tell the user someone is reading
                                           // their messages
                     + " reading messages.");

            }

            if (userCommand.equals("QUIT")) // If the user command is quit
            {
               quit = true; // Set quit to true
            }

            if (userCommand.equals("DELETE MESSAGE")) // If the user command is
                                                      // delete a message
            {
               int valueToDelete = 0; // Define local variables

               try {
                  valueToDelete = Integer.parseInt((String) input.readObject());
               } catch (NumberFormatException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } catch (ClassNotFoundException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
               } // Receive the value to delete
               Statement statement = null;
               String delete = "DELETE FROM emails WHERE [Email No] = "
                     + valueToDelete + "";
               try {
                  statement = link.createStatement();
                  statement.executeUpdate(delete);
                  link.commit();
               } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
               System.out.println("Deleted message " + valueToDelete); // Tell
                                                                       // the
                                                                       // user a
                                                                       // message
                                                                       // has
                                                                       // been
                                                                       // deleted
            }
         } while (!quit); // Run while quit is false

         System.out.println("Client Closed"); // Tell the user the client has // closed
      }

      private static void getFile(ObjectInputStream inStream, String fileType)
            throws IOException, ClassNotFoundException {

         byte[] byteArray = (byte[]) inStream.readObject();
         FileOutputStream mediaStream = null;

         if (fileType.equals("Text File"))
            mediaStream = new FileOutputStream("file.txt");
         else if (fileType.equals("Image"))
            mediaStream = new FileOutputStream("file.gif");
         else if (fileType.equals("Movie"))
            mediaStream = new FileOutputStream("file.mpeg");

         mediaStream.write(byteArray);
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:33:34+00:00Added an answer on June 2, 2026 at 11:33 pm

    When you are instantiating the ObjectInputStream it will block until the client sends a sufficient header, thus you never get to actually run the thread… See also

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a basic java console based menu system. I have
I'm trying to make a basic datagram client/server program with Java. I've made the
I am trying to make a basic authentication system in iOS that sends a
I'm trying to make a basic application that displays an image from the camera,
I'm trying to make a basic client-server application for windows phone 7 (using Mango
I am trying to make a basic iphone app that shows nearby tweets. I
I'm trying to make a basic model of the solar system in Direct X.
I am trying to make LDAP queries via Visual Basic. I don't have administrator
I'm trying to make a basic Windows application that builds a string out of
I'm trying to make a basic counter. The idea is that the user presses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.