Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Very simple chat program hanging

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Very simple chat program hanging

    This is the code. it seems to be a problem with the threads not the socket connection, when i run it it just uses all the cpu and hangs Help me please i`m loosing my mind over this coz i can`t really find something wrong with it and it is due tomorrow...

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package chat;
     
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
     
    /**
     *
     * @author taghack
     */
    class Read extends Thread
    {
        BufferedReader in;
        public Read(BufferedReader in)
        {
            this.in=in;
        }
        @Override
        public void run()
        {
            try
            {
            while(true)
            {
            if(in.ready())
            {
            try
            {
            //byte[] tmparr=new byte[100000];
            //in.read(tmparr);
            System.out.println(in.readLine());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            }
            }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    class Write extends Thread
    {
        PrintWriter out;
        public Write(PrintWriter out)
        {
            this.out=out;
        }
        @Override
        public void run()
        {
            //byte[] tmparr=new byte[100000];
            while(true)
            {
            if(Main.consoleinput.hasNext())
            {
            try
            {
            String message;
            message=Main.consoleinput.next();
            out.println(message);
            out.flush();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            }
            }
        }
    }
    public class Main {
           public static String nickname;
           public static Scanner consoleinput=new Scanner(System.in);
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)  {
            try
            {
            // TODO code application logic here
            System.out.println("Please input your nickname:");
            nickname=consoleinput.nextLine();
            System.out.println("Please input the ip of the other participant in the chat session");
            String ip=consoleinput.nextLine();
            boolean surverisrunning=false;
            Socket mainsocket;
            int numberoftries=0;
            do
            {
                try
                {
                    numberoftries++;
                    System.out.println("trying to connect "+numberoftries);
                    Socket temp=new Socket(ip,3000);
                    surverisrunning=true;
                    temp.close();
                }
     
                catch(Exception e)
                {
                    surverisrunning=false;
                    e.printStackTrace();
                }
            }
            while(surverisrunning==false && numberoftries<10);
     
            if(surverisrunning)
            {
                mainsocket=new Socket(ip,3000);
                System.out.println("CONNECTION ESTABLISHED AS CLIENT!");
            }
            else
            {
                System.out.println("PleaseWainForConnctionFromClient");
                ServerSocket a=new ServerSocket(3000);
                mainsocket=a.accept();
                System.out.println("CONNECTION ESTABLISHED AS SERVER!");
                surverisrunning=true;
            }
            BufferedReader in=new BufferedReader(new InputStreamReader(mainsocket.getInputStream()));
            PrintWriter out=new PrintWriter(new OutputStreamWriter(mainsocket.getOutputStream()));
            Read read1=new Read(in);
            Write write1=new Write(out);
            read1.start();
            write1.start();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
     
    }
    Last edited by copeg; November 3rd, 2011 at 08:46 PM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Very simple chat program hanging

    Read the error messages....

    Connection refused: connect
    trying to connect 2
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(Unknown Source)
    	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at chat.Main.main(Main.java:103)
    trying to connect 3
    java.net.ConnectException: Connection refused: connect
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(Unknown Source)
    	at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at chat.Main.main(Main.java:103)
    java.net.ConnectException: Connection refused: connect

    The exception says that there is no listening socket at the address
    and port number you attempted to connect to.

    Is the process running? Is it listening where you think it is? Are you
    connecting where you think you are? Most of these things can be
    diagnosed with e.g. netstat on the target machine and a network
    snooping tool like tcpdump or ethereal.
    java.net.ConnectException: Connection refused

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very simple chat program hanging

    well i know about that first set of 10 errors they are simply a set of 10 attempts to connect to a server to see if there si one running on the target machine and if not the code goes on to establish a rurver itsself so that is not a problem. i am pretty sure that the two isntances of the program (currently testing one one pc running two times) do connest to each other the problem seems to come from the threads maybe tey are in sone sort of weird deadlock (well not exactly) but i cant seem to figure it out

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Very simple chat program hanging

    Quote Originally Posted by macko
    Read the error messages....
    The original poster did not mention this exception, and given you don't have the ip of the server you should not assume this is related to the problem at hand.

    taghack, please wrap your code in the code tags (I edited your post for you). Second
    Quote Originally Posted by taghack
    when i run it it just uses all the cpu and hangs
    Hangs where? Add some println's in there to determine where it hangs to debug the flow of the code. We don't have a server to connect to, so if your program hangs beyond that point you need to give us more information, such as where it 'hangs', what the server sends, etc...

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very simple chat program hanging

    thanks for the edit (not familiar with the forum sry) the connection seems to play out just fine
    System.out.println("CONNECTION ESTABLISHED AS CLIENT!");
    System.out.println("CONNECTION ESTABLISHED AS SERVER!");
    these two messages are right after the attempts to connect the two sockets together and should only display if the connection is ok and they do in their respective instances of the program so i really think its a matter of threads. otherwise it mamages to start the two threads after which netbeans hangs from lack of cpu no error code no nothing

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Very simple chat program hanging

    If the server is online can you connect via the client?

    If the client doesn't connect when the servers off ?

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very simple chat program hanging

    well the idea is that the program can be both a client and a server it checks it there is a server on the other machine( the 10 errors) and if not opens a server socket itsself and waits for a connection but i think that that part is actually working well

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very simple chat program hanging

    The issue is still at hand going to rest now but anny suggestion will be greatly appreciated ! Thank you all VERY VERY much for your help !

Similar Threads

  1. Simple client-server chat program
    By Saloni Patil in forum Java Networking
    Replies: 3
    Last Post: October 22nd, 2011, 09:29 AM
  2. Object Stream Hanging...
    By Ellipsis in forum Java Networking
    Replies: 2
    Last Post: October 22nd, 2010, 07:20 PM
  3. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  4. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM
  5. Java LAN Chat Program
    By nevilltron in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2010, 03:15 AM