Results 1 to 17 of 17
  1. #1
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default [SOLVED] TCP chat test availability server

    Hallo,
    I did these programs:
    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Lolek
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            String addressIP;
            if(args.length==0)
            {
                addressIP="127.0.0.1";
            }
            else
            {
                addressIP=args[0];
            }
            ReciveT recive = new ReciveT();
            SendT send = new SendT(addressIP);
            
            recive.server.start();
            send.client.start();
        }
    
    }
    
    class ReciveT implements Runnable
    {
      Thread server;  
      
      public ReciveT()
      {
          server = new Thread(this,"Server Thread");
      }
      public void run()
      {
            try {
                ServerSocket serverSocket = new ServerSocket(5000);
                Socket clientSocket = null;
                clientSocket = serverSocket.accept();
                while(true)
                {
                    BufferedReader read = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                    String line = null;
                        while((line = read.readLine())!=null)
                        {
                            System.out.println(line);
                            System.out.flush();
                        }
                }
                
            } catch (IOException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            }
          
        
      }
    }
    
    class SendT implements Runnable
    {   
        private String addressIP;  
        Thread client;
        
        public SendT(String ip)
        {   
            this.addressIP=ip;
            client = new Thread(this,"Client Thread");
        }
        public void run()
        {
            try {
                Thread.sleep(10000);
                
                InetSocketAddress server = new InetSocketAddress(this.addressIP, 4000);
                                 
                Socket socket = new Socket(this.addressIP, 4000);
    
                BufferedReader readKey = new BufferedReader(new InputStreamReader(System.in));
    
                OutputStream output = socket.getOutputStream();
                BufferedWriter write = new BufferedWriter(new OutputStreamWriter(output));
    
                String line;
                while ((line = readKey.readLine()) != null) {
                    write.write(line);
                    write.newLine();
                    write.flush();
                    System.out.println("Text:" + line);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnknownHostException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            
                
             
        }
    }
    and second:
    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Lolek
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            String addressIP;
            if(args.length==0)
            {
                addressIP="127.0.0.1";
            }
            else
            {
                addressIP=args[0];
            }
            ReciveT recive = new ReciveT();
            SendT send = new SendT(addressIP);
            
            recive.server.start();
            send.client.start();
        }
    
    }
    
    class ReciveT implements Runnable
    {
      Thread server;  
      
      public ReciveT()
      {
          server = new Thread(this,"Server Thread");
      }
      public void run()
      {
            try {
                ServerSocket serverSocket = new ServerSocket(4000);
                Socket clientSocket = null;
                clientSocket = serverSocket.accept();
                while(true)
                {
                    BufferedReader read = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                    String line = null;
                        while((line = read.readLine())!=null)
                        {
                            System.out.println(line);
                            System.out.flush();
                        }
                }
                
            } catch (IOException ex) {
                Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
            }
          
        
      }
    }
    
    class SendT implements Runnable
    {   
        private String addressIP;  
        Thread client;
        
        public SendT(String ip)
        {   
            this.addressIP=ip;
            client = new Thread(this,"Client Thread");
        }
        public void run()
        {
            try {
                Thread.sleep(10000);
                
                InetSocketAddress server = new InetSocketAddress(this.addressIP, 5000);
                                 
                Socket socket = new Socket(this.addressIP, 5000);
    
                BufferedReader readKey = new BufferedReader(new InputStreamReader(System.in));
    
                OutputStream output = socket.getOutputStream();
                BufferedWriter write = new BufferedWriter(new OutputStreamWriter(output));
    
                String line;
                while ((line = readKey.readLine()) != null) {
                    write.write(line);
                    write.newLine();
                    write.flush();
                    System.out.println("Text:" + line);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnknownHostException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            
                
             
        }
    }
    And i have this question. How can test availability server, something in place of Thread.sleep(10000);?

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    What does "test availability server" mean?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    Trying if the server is reachable.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You attempt to connect and see if it works, just as you have done. No need for the sleep().
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    And How can i do this thing? I am beginer. Becouse now this error: java.net.ConnectException: Connection refused: connect

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Catch the error and deal with it. If there's nothing you can do then just print the error message.

    Further points:
    You don't need to flush System.out
    You do need to close the socket once you're done with it
    You need to move serverSocket.accept() into the while loop
    You need to have both the server and the client use the same port
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    I think you donītīunderstand my code or I donīt understand you. My program is client and server too and connect on same program because ServerT need diferent port number and ClientT need diferent port number.

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If you're trying to get SendT to talk to RecieveT they have to connect to the same port. A client and server can only communicate if they are using the same port. It's also much easier to test this program if you make the server and client into separate applications, then you can start the client once you know the server is set up.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    No I donīt want this. I know ports and this stuff. This program copy to another PC and connect together. I donīt need central server becouse every program is server also client. OK? Sory for my English.

  10. #10
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then why have you hard-coded the port in 2 complete code duplicates? Use command line args.

    But why not just use one connection? Sockets have both input and output streams. Having a different port number for every client is going to be unmanageable.

    The solution to your original problem is to either have a command that tries to connect, or just retry the connection attempt until successful.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  11. #11
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    I think that, Every client donīt need different port number it is just on localhost. And if i use just one connection, One program was server and second client or no? I do this for small LAN about 10 computers no big deal.
    And could you write sample code how can i do this stuff? Thanks

  12. #12
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Ignore that one connection comment - wasn't thinking properly.

    Here's your current set-up: Client 1 listens on port x and sends to client 2 on port y. Client 2 listens on port y and sends to client 1 on port x. What is client 3 supposed to do?

    What you want is for every client to listen and connect to the same port number (ServerSocket and Socket bound to same port). Then any client can talk to any other. You won't be able to run two copies on the same machine, but then why would you want to.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  13. #13
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    Yes i know. Communication can be only between 2 computers. But if I want that, Communication was between more computers, I must to creat for each client new Thread. Maybe i will do this later. But now i want do just very sipmle chat. I am beginner, I program just 2 months.

  14. #14
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I have already provided solutions that will get your test working.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  15. #15
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    And could you write sample code? I would very grateful.

  16. #16
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Sample code for what?

    Java Code:
    try {
        while (true) {
            try {
                socket = new Socket(this.addressIP, PORT);
                break;
            }
            catch (IOException e) {
                System.err.println(e.getMessage);
                Thread.sleep(10000);
            }
        }
    }
    catch (InterruptedException e) {
        System.out.println("Terminating on INT");
        return;
    }
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  17. #17
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

Similar Threads

  1. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 12:22 AM
  2. [SOLVED] UDP chat client server
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 04-25-2009, 01:51 AM
  3. Server.java Errors trying to build a chat
    By agfre44_9873 in forum Java Applets
    Replies: 2
    Last Post: 04-25-2009, 01:49 AM
  4. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM
  5. JDK availability in Testing server
    By curesh in forum Suggestions & Feedback
    Replies: 6
    Last Post: 01-26-2009, 03:33 AM

Posting Permissions

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