Results 1 to 6 of 6
  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Exclamation Multithreading in java

    Greetings to all,

    I am using a thread to read messages over a socket which allows a person to type while the thread reads the messages from the other client. Though there is a problem, it only reads the first argument (i.e One single String and not a line for example if I type "How are you?" it only reads "How" on the other computer. Also the second problem is that the thread misses out on every next message, for example if I typed "Hello" the first time and then type "how are you" ? then afterward typed "where are you?". The Only ones that appears on the other computer are "Hello" and "Where are you?".

    I have a clue for the first problem but have no idea why the second one is doing this, do I need to create like a protocol to specify heads and ends of messages for this?

    I am using Object[Input/Output]Stream to read and write

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Could we see your code that reads and writes the messages? Also, are you using TCP or UDP?

  3. #3
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Thanks for the Quick Reply, I am using TCP .
    This is the method I used to start the server
    Java Code:
     public void start(int port)
        {
         
         
         try
         {
           //create socket and listen for connections
           //String addr = iadd.toString();
           System.out.printf("\nCreating Socket on port %d ", port);
           System.out.println("\nListening...");
           ss = new ServerSocket(port);
           //accept connection
           s =  ss.accept();
    
          
         
         }
         catch(IOException ioe)
         {
          System.out.println("An I/O Error Occured ");
          ioe.printStackTrace();
         }
         catch (Exception e)
         {
          System.out.println("Some Error occcured");
          e.printStackTrace();
         }
         
         Thread nc = new Thread( new CommHandler(s));
         nc.start();
        
        }
    this is the class that handles communication with the client while the server listens.
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    
    public class CommHandler implements Runnable
    {
      ObjectInputStream in;
      ObjectOutputStream out;
      Socket clientSocket;
      Scanner scan;
      String buffer;
      public CommHandler()
      {//no args
      }
       
        //Takes in the clients socket
      public CommHandler(Socket c)
      {
       
        
        setUpConnection(c);
          
        
      }
      public void setUpConnection(Socket c)
      {
         clientSocket = c;
        try
        {
          in  = new ObjectInputStream( c.getInputStream());
          out = new ObjectOutputStream( c.getOutputStream());
          out.flush();
          
        }catch(IOException ioe)
        {
          System.out.println("Cannot connect!!! I/O Error");
          
        }
        catch(Exception e)
        {
          System.out.println("Some Error Occured!!!");
        }
      }
      public void run()
      {
        talk();
      }
      //handles communication witht the client
      public void talk()
      {
        
        scan = new Scanner(System.in);
        Thread readMsg = new Thread(){
         public void run()
         {
           String tmp;
           try
           {
             while(clientSocket.isConnected())
             {
               if((!in.readObject().equals(null)))
               {
                 
                        while((tmp = (String)in.readObject()).equals("*"))
                        {
                          buffer += (String)in.readObject();
                        }
                        System.out.println(buffer);
               
               }
             }
           }catch(Exception e)
           {
            System.out.println("Read Error!!!"); 
           }
           
         }
        };
        
         readMsg.start();
         try
         {
           while(clientSocket.isConnected())
           {
        
             System.out.println("Type a Message : ");
             out.writeObject(scan.next());
         
           }
            //close connections
       out.close();
       in.close();
         }catch(Exception e)
         {
           System.out.println("Write Error!!!"); 
         }
       
     
       
        
      }
        
        
        
      
      
    }
    Last edited by MuslimCoder; 02-20-2010 at 07:15 AM.

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    In the first code you gave, while the connection is established, it waits for the user to type something, and then reads from the object input stream. It will not read anything unless it writes first, and even then, will keep alternateing. You need to make another thread to handle input or output on BOTH sides of the connection.

  5. #5
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    hmm.. I am not sure, I started the tread for reading the mesages before the write...look
    Java Code:
    readMsg.start();
         try
         {
           while(clientSocket.isConnected())
           {
        
             System.out.println("Type a Message : ");
             out.writeObject(scan.next());
         
           }
            //close connections
       out.close();
       in.close();
         }catch(Exception e)
         {
           System.out.println("Write Error!!!"); 
         }
    I wanted the thread ReadMsg to handle any incoming messages while the user is typing in the interface but apparently it seems to lag in a uniform way by skipping the next read after reading one, which tell me its a bug . I would try using a a buffer reader and a printWriter.

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I'm not quite sure. When I made this, I used a buffered reader and buffered writer.

Similar Threads

  1. Log 4j Multithreading
    By joe2010 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 03:48 AM
  2. Applet and multithreading
    By pricelessjunk in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 09:47 PM
  3. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •