I made a simple client-server chat programs using sockets and so far it works, then I tried to switch the programs around to see the connection with VB program (made by my colleague) and finally get a VB client-Java Server works...however on the other way around with Java as client, I get connection timed out.


Here's the code :

Java Code:
    public static void main(String[] args) throws UnknownHostException {

        Socket clientSocket = null;
        DataOutputStream os = null;
        DataInputStream is = null;
        PrintStream output;
        InetAddress addr = InetAddress.getByName("192.168.1.140");
        String hostname = addr.getHostName();

        try {
            clientSocket = new Socket(hostname, 11111);
            os.writeBytes("Socket connection : " + clientSocket);
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + hostname);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + hostname);
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        if (clientSocket != null && os != null && is != null) {
            try {
                while (true) {
                    System.out.print("Client: ");
                    Scanner scInput = new Scanner(System.in);
                    String sInput = scInput.nextLine();
                    os.writeBytes(sInput);
                    output = new PrintStream(clientSocket.getOutputStream());
                    String responseLine;
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                    while ((responseLine = in.readLine()) != null && !responseLine.equalsIgnoreCase("bye")) {
                        System.out.println("Server: " + responseLine);
                        System.out.print("Client: ");
                        Scanner scInputX = new Scanner(System.in);
                        String sInputX = scInputX.nextLine();
                        os.writeBytes(sInputX);
                        if (responseLine.indexOf("Ok") != -1) {
                            break;
                        }
                    }
                    os.close();
                    System.out.println("os closed");
                    is.close();
                    System.out.println("is closed");
                    clientSocket.close();
                    System.out.println("client socket closed");
                }
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }
and here's the error message in console (the vb server program runs and I'd checked that the port used is on listening state using netstat) :

Couldn't get I/O for the connection to: 192.168.1.140
Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Na tive Method)
at java.net.AbstractPlainSocketImpl.doConnect(Abstrac tPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress( AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractP lainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at javaapp2.JavaApp2.main(JavaApp2.java:27)

I don't see any problem since the same code works with my java server program, but maybe someone can see the problem here.

Second to the problem, I want to improve this, the currently running (with Java as both client and server), I can only make it as far as either server/client starts the chat...I want to be able to make it kind of simultaneous so that while it listens it can also starts. So far as my guess, it may have to do with threading, but if there's anything I'd like to hear it.


Thanks in advance,