Results 1 to 3 of 3
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation Client cannot receive inputstream from server

    I have a server and client programmed under MVC.
    When both server and clients were ran on localhost and the same port;
    the client communicate to server but the server cannot communicate to the client.

    Here's the code of the Model.java. What code should I alter to fix the problem?

    Thanks

    Model.java
    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import java.net.InetAddress;
    import java.net.Socket;
    
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class Model {
    
        private Socket socket;
        private String computerName;
        private String serverIPAdd;
        private int portNumber;
        private ObjectInputStream input;
        private ObjectOutputStream output;
        private JTextArea messageField;
    
        public Model(String computerName, String serverIPAdd, int portNumber, JTextArea messageField) {
            this.computerName = computerName;
            this.serverIPAdd = serverIPAdd;
            this.portNumber = portNumber;
            this.messageField = messageField;
        }
    
        public void runClient() {
            try {
                connect();
                recieveMessage();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        public void close() {
            try {
                socket.close();
                input.close();
                output.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private void connect() throws IOException {
            socket = new Socket(InetAddress.getByName(serverIPAdd), portNumber);
            input = new ObjectInputStream(socket.getInputStream());
            output = new ObjectOutputStream(socket.getOutputStream());
            output.flush();
            display("Connected to Server.");
        }
    
        private void recieveMessage() throws IOException {
            try {
                String message;
                do {
                    message = (String)input.readObject();
                    display(message);
                } while(message.contains("Terminate"));
            } catch(ClassNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    
        protected void sendMessage(final String message) {
            try {
                String data = computerName + ": " + message;
                output.writeObject(data);
                output.flush();
                display(data);
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    
        private void display(final String message) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    messageField.append(message + "\n");
                }
            });
        }    
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default

    the server cannot communicate to the client.
    Are there errors? What does the server do? Does it receive anything from the client?
    What does the server do after it receives data from the client?
    Could the client be the problem? It's not getting what the server sent?

    How does the Model code you posted relate to the problem?

  3. #3
    vdparaliya is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Hello Friend
    I think u have to use a thread in ObjectInputStream
    it can solve your problem if you dont understand then
    check I have post here a Personal Messenger use that coding I have
    writen it will help you...

Similar Threads

  1. How to receive byte array from Server?
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-18-2010, 08:37 AM
  2. Replies: 0
    Last Post: 12-02-2008, 10:55 AM
  3. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  4. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 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
  •