0

I am designing a chat application using sockets. this is the code for reading data from server. It compiles just fine but the thread only runs only once. please help me out.

public void reader() {
    Thread read=new Thread(){
        public void run(){
            try {
                while(true) {
                    if(in.readLine()!=null) {
                        System.out.println("Recived Message: "+ in.readLine());
                    }
                }
            }catch(Exception e){}
        }
    };

    read.setPriority(10);
    read.start();
} 

Ok I tried this code and it doesnt work as well

public void reader()
{
    Thread u=new Thread()
        {
        public void run()
            {
            try {
            while(true)
            {
                System.out.println("ssss");
                if(input.readLine()!=null)
                {
                    String message=input.readLine();
                    System.out.println("SERVER:"+message);
                }
                else{
                    Thread.sleep(1);
                }

            } 
            }
            catch (IOException e)

                e.printStackTrace();
            } catch (InterruptedException e) 
            {
                e.printStackTrace();
            }

            }

    };
    try{
    u.start();
    }catch(Exception e){}
    }

And the output i get is sss just once. but i have a while loop that is always true. why doesnt ir run infinitely?

3 Answers3

1

(can't comment due to low reputation...)

Better read line this way:

String line = "";
while( (line = in.readLine()) != null) {
    System.out.println("Recived Message: " + line);
}

PS: Take care, you are calling 2 times readLine()

exoddus
  • 2,230
  • 17
  • 27
  • I tried this way too. but still the while loop runs only once – Kartik Gupta May 29 '14 at 08:37
  • then readLine is just reading 1 line or is returning null after that. Maybe printing the catch block could give you some tips: catch(Exception e){ e.getMessage()} – exoddus May 29 '14 at 08:55
1

What you need to do is remove the catch and the IOException

If you need a Thread like that more than once you should consider to encapsulate it in a class.

Like seen here:

public class Foo extends Thread
{
    public Foo()
    {
        super("Foo");
    }

    @Override
    public void run()
    {

        while(!isInterrupted())
        {
            System.out.println("Still running");

            try
            {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) 
    {
        Thread foo = new Foo();
        foo.start();
    }
}

Otherwise you can consider implementing the Runnable Interface as it is seen to be the preferred way of handling with Threads in Java.

For further information why it is please have a look at:

"implements Runnable" vs. "extends Thread"

Hope this helps.

Community
  • 1
  • 1
herby
  • 389
  • 3
  • 8
0

You call readLine twice and in each call it fetches the next line so the first is reading the line and the second has nothing to read so it stuck

Hovav
  • 151
  • 2
  • 13