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

    Exclamation ConnectException?

    Strange, I never know how to resolve this.
    I have created a server, in which accepts multi-clients, and client using MVC.
    I have threaded some parts of the sockets.
    Yet, I recieve this error.

    Java Code:
    java.net.ConnectException: Connection refused: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
            at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
            at java.net.Socket.connect(Socket.java:519)
            at java.net.Socket.connect(Socket.java:469)
            at java.net.Socket.<init>(Socket.java:366)
            at java.net.Socket.<init>(Socket.java:209)
    ...
    This exception always pertains to this part of the client
    Java Code:
    socket = new Socket(InetAddress.getByName(serverIPAdd), portNumber);
    And here's the Server Model
    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.SwingUtilities;
    import javax.swing.JTextArea;
    
    public class Model {
    
        private ServerSocket server;    
        private Computer[] computer;
        private int portNumber;
        private int queueLength;
        private JTextArea messageField;
        private Thread thread;
    
        public Model(int portNumber, int queueLength, JTextArea messageField) {
            System.out.println("Model start");
            this.portNumber = portNumber;
            this.queueLength = queueLength;
            this.messageField = messageField;
            computer = new Computer[queueLength];
            System.out.println("Model end");
        }
        
        public void runServer()  {
            System.out.println("runServer start");
            thread = new Thread() {
                public void run() {
                    try {
                        server = new ServerSocket(portNumber, queueLength);
                        display("Server started");
                        display("Awaiting connection...");
                        for(int index=0; index<computer.length; index++) {
                            computer[index] = new Computer(server.accept());
                            computer[index].start();
                        }
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            System.out.println("runServer end");
        }
    
        public void sendMessage(final String message) {
            System.out.println("sendMessage start");
            try {
                for(int index=0; index<computer.length; index++) {
                    computer[index].sendMessage(message);
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
            System.out.println("sendMessage end");
        }
    
        public void closeServer() {
            System.out.println("closeServer start");
            try {           
                for(int index=0; index<computer.length; index++) {
                    computer[index].close();
                }
                server.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
            thread.stop();
            System.out.println("closeServer end");
        }
    
        private void display(final String message) {
            System.out.println("display start");
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    messageField.append(message + "\n");
                }
            });
            System.out.println("disply end");
        }
    
        private class Computer extends Thread {
    
            private Socket socket;
            private ObjectInputStream input;
            private ObjectOutputStream output;
    
            public Computer(Socket socket) {
                System.out.println("Computer start");
                this.socket = socket;
                System.out.println("Computer end");
            }
            
            @Override
            public void run() {
                System.out.println("run start");
                try {
                    input = new ObjectInputStream(socket.getInputStream());
                    output = new ObjectOutputStream(socket.getOutputStream());
                    recieveMessage();
                } catch(IOException e) {
                    e.printStackTrace();
                }
                System.out.println("run end");
            }
    
            private void recieveMessage() throws IOException {
                System.out.println("recieveMessage start");
                try {
                    String message;
                    do {
                        message = (String)input.readObject();
                        display(message);
                    } while(message.contains("Terminate"));
                } catch(ClassNotFoundException e) {
                    e.printStackTrace();
                }
                System.out.println("recieveMessage end");
            }
    
            public void sendMessage(final String message) throws IOException {
                System.out.println("sendMessage start");
                String data = "Server: " + message;
                output.writeObject(data);
                output.flush();
                display(data);
                System.out.println("sendMessage end");
            }          
    
            public void close() {
                System.out.println("close start");
                try {
                    socket.close();
                    input.close();
                    output.close();
                } catch(IOException e) {
                    e.printStackTrace();
                }
                System.out.println("close end");
            }
    
        }    
    }
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Jononomous is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default

    Looks like you're behind a firewall or something of that sort. A refused connection means it was successful in finding the destination, but the destination is saying that it isn't going to allow that connection. Are you running a nix/windows FW? Try disabling all of them (if running more than one anyway) and see what happens.

    Regards

Similar Threads

  1. Replies: 3
    Last Post: 01-26-2009, 06:30 PM
  2. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  3. ConnectException not found
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 09:23 AM
  4. java.net.ConnectException: Connection refused
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-03-2007, 05:49 PM

Posting Permissions

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