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.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 52

Thread: can someone help me in this server and multi client

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    it only can run till read
    Where does the server write what the client is trying to read? Does the debug output show that the server has written something for the client to read? If the server hasn't written something for the client to read, what is the problem that it doesn't write something?


    The posted code does not have enough debug println statements in it. Only two messages are shown on the console

    YOU NEED TO ADD LOTS OF println() STATEMENTS TO SHOW THE EXECUTION FLOW AND WHAT VALUES ARE SENT AND RECEIVED!!!!
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

     public void run(){
                try{
                while(true){               
                  String msgtoreturn = "";
                     int  log = inputFromClient.readInt();         
     
                            allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF();
                        jtaDisplay.append( allmessage[chatlog] + "\n");
                        chatlog = chatlog+1;             
                       outputToClient.writeInt(chatlog);
     
                      for(int i = log;i<chatlog;i++){
                           msgtoreturn = msgtoreturn + allmessage[i] + "\n";
                      }
                        System.out.print(msgtoreturn);
                       outputToClient.writeUTF(msgtoreturn);
                     outputToClient.flush();
     
                    }
                }catch(IOException ex){
                    System.err.println(ex);
                }
            }


    server there have outputToClient.writeInt(chatlog); AND outputToClient.writeUTF(msgtoreturn);

    The server have written thing for the client to read. first client and second client read successful but after second client read and use back first client to read ,it seem like not successful to read already

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    Post the debug println output that shows what happens.

    There should be a line printed for all the events in the program. Before and after all reads and writes showing what was read and what was written. Each client should be assigned an id so you can tell which client is printing the message. There needs to be a way to connect the HandleAClient class with the Client it is connected to. That can be done by printing out the socket they are using.

    Here is a sample of the debug printlns for the earlier version of the program:
    Running: java ServerClient

    200 C - connected socket=Socket[addr=/127.0.0.1,port=8080,localport=51232]
    500 C - connected socket=Socket[addr=/127.0.0.1,port=8080,localport=51233]
    S - waiting for connect
    S - connected
    S - writing 0
    200 C - read log=0
    S - waiting for connect
    S - connected
    S - writing 0
    500 C - read log=0
    S - waiting for connect
    0HAC - run() socket=Socket[addr=/127.0.0.1,port=51232,localport=8080]
    0HAC - reading
    1HAC - run() socket=Socket[addr=/127.0.0.1,port=51233,localport=8080]
    1HAC - reading
    500 C - writing log=0
    500 C - writing msg=1-550<
    1HAC - read log=1329
    0HAC - read log=12589
    1HAC - writing chatlog=1
    0HAC - writing chatlog=2
    1HAC - writing mtr=<
    0HAC - writing mtr=<
    0HAC - reading
    1HAC - reading
    500 C - reading1
    500 C - read log=1
    500 C - reading2
    500 C - read allmessage=<
    200 C - writing log=0
    200 C - writing msg=1-200<
    200 C - reading1
    >>>>>>>>Exiting main

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    i not really good at debug but i still try it, i just realize that after the second client run then back to first client to run..it stop at log = fromServer.readInt(); , but the server there is stop at allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF();
    seem like server try to read from the client at the same time client try to read from server.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    server try to read from the client at the same time client try to read from server.
    What does the debug print out show about which program wrote first and which program is trying to read?
    What order does the program need to do the writes and reads in?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    the debug show the server read first then followed by client read, then it stop already.
    program is for client to enter the data..then the server read, after read server update the data then give back to the client

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    the server read first then followed by client read
    Where are the writes being done for those reads?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    example like the server read the Int from the client so that the server can store the message inside String array at the server .
    and let say if the second client already got input in the server ,then the Int help to server to know which message need to write to the first client(the message input by second client).

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    Does the debug output show that something is being written before the other side tries to read?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    nope...the last two statement is server and client try to read each other

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    last two statement is server and client try to read each other
    Where does the code write something to be read?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    at server
    allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF();
    try to read from client's
    toServer.writeUTF(jtfMessage.getText().trim());

    at client
    log = fromServer.readInt();
    try to receive updated data from server's
    outputToClient.writeInt(chatlog);

    this answered your question?

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    No. Just having code in the program does not mean that the code will be executed when it needs to be executed.

    What does the debug print outs show? Does it show there is a write done before there is a read?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    before server and client show they read each other int log = inputFromClient.readInt(); (in the server read successful), but when it come to allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF(); it fail to read but at the same the client there try to read from the server.

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    What does the debug printout show about the sequence of the events?
    Is there a write before or at the same time as the read?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    it show that the server's log = inputFromClient.readInt() read successful then it go to another read (allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF(); ) after that it jump to client's then stop at log = fromServer.readInt();

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    Please post the printout that shows when the writes and reads are being done and also shows what (the contents of the data) was written and read.

    The bits and pieces of code that you post are not helpful in understanding when those statements are execute or in what order they are executed. Data must be written before it can be read.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    Success entering
    reading log
    writing log to the server
    1
    writing message to the server
    message 3
    Success read log from client = 1
    reading msg from client store in allmessage[chatlog]
    read log

    SERVER
    public void run(){
                try{
                while(true){      
                    System.out.println("Success entering ");
                           String msgtoreturn = "";
          System.out.println("reading log");
                     int  log = inputFromClient.readInt();         
       System.out.println("Success read log from client  = "  + log);
         System.out.println("reading msg from client store in allmessage[chatlog]");
                        allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF();
                        jtaDisplay.append( allmessage[chatlog] + "\n");
                         System.out.println("readed msg from client stored in allmessage[chatlog]= " + allmessage[chatlog]);
                        chatlog = chatlog+1;     
                     System.out.println("before   write chatlog " + chatlog);
                       outputToClient.writeInt(chatlog);
                          System.out.println("after   write chatlog" + chatlog);
     
                      for(int i = log;i<chatlog;i++){
                           msgtoreturn = msgtoreturn + allmessage[i] + "\n";
                      }
                       System.out.println("before  write message  " + msgtoreturn);
                       outputToClient.writeUTF(msgtoreturn);
                       System.out.println("after  write message " + msgtoreturn);
                     outputToClient.flush();
     
                    }
                }catch(IOException ex){
                    System.err.println(ex);
                }
            }


    CLIENT
    public void actionPerformed(ActionEvent e){
                    try{
                   //    int logs =  fromServer.readInt();
                     //  String allmsg = fromServer.readUTF();
                     //  String host =  fromServer.readUTF();
                         System.out.println("writing log to the server");
                        toServer.writeInt(log);
                        System.out.println(log);
                        System.out.println("writing message to the server");
                        toServer.writeUTF(jtfMessage.getText().trim());
                        System.out.println(jtfMessage.getText().trim());
     
                        toServer.flush();        
           //             System.out.print(log);
             //             System.out.print(jtfMessage.getText().trim());
                        Thread t2 = new Thread(new Runnable() {
             public void run() {              
                readchatlog();
                readchatmessage();
                             }
          });
          t2.start();
     
                            jtfMessage.setText("");
                 }catch(IOException ex){
                        System.err.println(ex);
                    }
                }

    public void readchatlog(){
             try {    
                 System.out.println("read log");
                             log = fromServer.readInt();
                               System.out.println("log "  +  "\t" + log);
                         } catch (IOException ex) {
                             Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                         }
        }
        public void readchatmessage(){
             try {    
                  System.out.println("read msg");
                             allmessage = fromServer.readUTF();
                              jtaDisplay.append(allmessage);
                               System.out.println("message "  +  "\t" + allmessage);
     
                         } catch (IOException ex) {
                             Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                         }
        }

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    That debug printout does not say enough about what is happening.
    What value was read for the log?
    What value was written for the message?
    Which client sent the message?
    Which message was received from what client?

    There is no way I can use the posted code for testing. You need to post all of the code that will compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    SERVER
     
    import java.awt.BorderLayout;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Date;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
     
    public class MultithreadServer extends JFrame{
        private JTextArea jtaDisplay = new JTextArea();
        InetAddress inetAddress;
        String[] allmessage = new String[1000000];
       int chatlog=0;
     
        DataOutputStream outputToClient;
        DataInputStream inputFromClient;
     
     
        public MultithreadServer(){
             jtaDisplay.setEditable(false);
            add(new JScrollPane(jtaDisplay), BorderLayout.CENTER);
            setTitle("Multi-Thread Server");
            setSize(500,300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
     
            try{
     
                ServerSocket serverSocket = new ServerSocket(8000);
                jtaDisplay.append("Multi-Thread Server start at " + new Date() + "\n");
     
                int clientNo = 1;
                while(true){
                    Socket socket = serverSocket.accept();
                            // Find the client's host name, and IP address
            inetAddress = socket.getInetAddress();
              outputToClient = new DataOutputStream(socket.getOutputStream());
             inputFromClient = new DataInputStream(socket.getInputStream());  
     
                   outputToClient.writeInt(chatlog);
     
                    HandleAClient task = new HandleAClient(socket);
                    new Thread(task).start();
                }
            }catch(IOException ex){
               System.err.println(ex);
            }
        }
     
        public static void main(String[] args){
            new MultithreadServer();
        }
     
        class HandleAClient implements Runnable{
            private Socket socket;
     
            public HandleAClient(Socket socket){
                this.socket = socket;
            }
     
            public void run(){
                try{
                while(true){      
                    System.out.println("Success entering ");
                           String msgtoreturn = "";
          System.out.println("reading log");
                     int  log = inputFromClient.readInt();         
       System.out.println("Success read log from client  = "  + log);
         System.out.println("reading msg from client store in allmessage[chatlog]");
                        allmessage[chatlog] = inetAddress.getHostName() + "\t" +inputFromClient.readUTF();
                        jtaDisplay.append( allmessage[chatlog] + "\n");
                         System.out.println("readed msg from client stored in allmessage[chatlog]= " + allmessage[chatlog]);
                        chatlog = chatlog+1;     
                     System.out.println("before   write chatlog " + chatlog);
                       outputToClient.writeInt(chatlog);
                          System.out.println("after   write chatlog" + chatlog);
     
                      for(int i = log;i<chatlog;i++){
                           msgtoreturn = msgtoreturn + allmessage[i] + "\n";
                      }
                       System.out.println("before  write message  " + msgtoreturn);
                       outputToClient.writeUTF(msgtoreturn);
                       System.out.println("after  write message " + msgtoreturn);
                     outputToClient.flush();
     
                    }
                }catch(IOException ex){
                    System.err.println(ex);
                }
            }
     
        }
     
    }

    CLIENT
     
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
     
    public class Client extends JFrame{
        private JTextField jtfMessage = new JTextField();
        private JTextArea jtaDisplay = new JTextArea();
     
        private DataOutputStream toServer;
        private DataInputStream fromServer;
        int log;
          Socket socket ;
          String allmessage ;
     
     
        public Client(){
             jtaDisplay.setEditable(false);
            JPanel jp = new JPanel(new BorderLayout());
            jp.add(new JLabel("Enter Message: "), BorderLayout.WEST);
            jp.add(jtfMessage, BorderLayout.CENTER);
            jtfMessage.setHorizontalAlignment(JTextField.RIGHT);
     
            add(jp,BorderLayout.NORTH);
            add(new JScrollPane(jtaDisplay),BorderLayout.CENTER);
     
            setTitle("Client");
            setSize(500, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
     
            try{
                 socket = new Socket("localhost", 8000);    
                fromServer = new DataInputStream(socket.getInputStream());
                toServer = new DataOutputStream(socket.getOutputStream());
                log = fromServer.readInt();
     
     
            }catch(IOException ex){
                        jtaDisplay.append(ex.toString() + "\n");
            }
     
            jtfMessage.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    try{
                         System.out.println("writing log to the server");
                        toServer.writeInt(log);
                        System.out.println(log);
                        System.out.println("writing message to the server");
                        toServer.writeUTF(jtfMessage.getText().trim());
                        System.out.println(jtfMessage.getText().trim());                    
                        toServer.flush();        
     
                        Thread t2 = new Thread(new Runnable() {
             public void run() {              
                readchatlog();
                readchatmessage();
                             }
          });
          t2.start();
     
                            jtfMessage.setText("");
                 }catch(IOException ex){
                        System.err.println(ex);
                    }
                }
            });
        }
     
        public static void main (String[] args){
            new Client();
        }
         public void readchatlog(){
             try {    
                 System.out.println("read log");
                             log = fromServer.readInt();
                               System.out.println("log "  +  "\t" + log);
                         } catch (IOException ex) {
                             Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                         }
        }
        public void readchatmessage(){
             try {    
                  System.out.println("read msg");
                             allmessage = fromServer.readUTF();
                              jtaDisplay.append(allmessage);
                               System.out.println("message "  +  "\t" + allmessage);
     
                         } catch (IOException ex) {
                             Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                         }
        }
    }

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    Here's a sample of better debug printout. Each Client and HAC has a unique id so their messages can be identified. There is more that needs to be shown, but this is a start.
    Running: java ServerClient2

    1 C reading log
    0 C reading log
    S writing chatlog=0
    1 C read log=0
    0 HAC socket=Socket[addr=/127.0.0.1,port=52006,localport=8000]
    S writing chatlog=0
    1 HAC socket=Socket[addr=/127.0.0.1,port=52007,localport=8000]
    0 C read log=0
    0 HAC Success entering Socket[addr=/127.0.0.1,port=52006,localport=8000]
    0 HAC reading log
    1 HAC Success entering Socket[addr=/127.0.0.1,port=52007,localport=8000]
    1 HAC reading log
    0 C writing log to the server log=0
    0 C wrote 0
    0 C writing message to the server 1-200
    1 HAC Success read log from client = 0
    1 HAC reading msg from client store in allmessage[chatlog]
    0 C wrote msg=1-200
    0 HAC Success read log from client = 340269
    0 HAC reading msg from client store in allmessage[chatlog]
    0 C reading log
    1 C writing log to the server log=0
    1 C wrote 0
    1 C writing message to the server 1-500
    1 C wrote msg=1-500
    1 C reading log
    0 C writing log to the server log=0
    0 C wrote 0
    0 C writing message to the server 2-200
    0 C wrote msg=2-200
    0 C reading log
    1 C writing log to the server log=0
    1 C wrote 0
    1 C writing message to the server 2-500
    1 C wrote msg=2-500
    1 C reading log

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    your straight run 2 client? it run smoothly ?

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    No, my version does not execute correctly with 2 clients.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can someone help me in this server and multi client

    do you know the program got what problem?

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can someone help me in this server and multi client

    Look at the debug print out.
    Does the data that is read match what was written? For example If the client writes a number, does the server read that number?
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  2. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  3. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  4. Threads in multi client-server application
    By KingOfClubs in forum Threads
    Replies: 1
    Last Post: June 11th, 2011, 12:10 AM
  5. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM