0

I am creating a client-server as follows:

Server:

Listener thread daemon (listening always for incoming connections)

Service object that send/receive data with clients connected

Client(s):

There can be many instances of clients

However, on either sides after connection is established, it takes forever to create Constructor of type ObjectOutputStream & ObjectInputStream. Bit of googling revealed this and this. I followed steps of : 1. creating ObjectOutputStream first 2. flush it 3. creating ObjectInputStream second

But this doesn't work for me. Wonder why ??

Server:

Listener Thread/daemon:

Socket connSocket;
try {
        ServiceListener.serverSocket = new ServerSocket(listenPort);
        System.out.println("Listening on port: " + listenPort);

        while (true) {
            connSocket = serverSocket.accept();
            nodeList.add(connSocket);
            System.out.println("Accepted connections ( " + connSocket + "):" + getConnectedNodeCount());
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Server Thread (After accepting Connection)

for (int i=0; i<listenerObj.getConnectedNodeCount(); i++) {
        try {
            System.out.println(listenerObj.nodeList.get(i));
            serviceTx = new ObjectOutputStream(listenerObj.nodeList.get(i).getOutputStream());
            serviceTx.flush();
            serviceRx = new ObjectInputStream(listenerObj.nodeList.get(i).getInputStream());

            String rxMsg = (String) serviceRx.readObject();
            if (rxMsg.equals("HELLO")) {
                System.out.println("Service received: " + rxMsg);
                serviceTx.writeObject((Object) "HELLO");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Client:

    try {
        System.out.println("Creating Node socket...");
        clientSocket = new Socket(getServerIp(), getServerPort());

        nodeTx = new ObjectOutputStream(clientSocket.getOutputStream());
        nodeTx.flush();
        nodeRx = new ObjectInputStream(clientSocket.getInputStream());

        System.out.println("Connected to " + getServerIp() + ":" + getServerPort());

        do {
            String outBoundMsg = new String();
            outBoundMsg = "HELLO";
            System.out.println("Node sending \"" + outBoundMsg + "\" to service");
            nodeTx.writeObject(outBoundMsg);
            nodeTx.flush();

            String rcvdMsg = (String) nodeRx.readObject();
            if(rcvdMsg.equals("HELLO")) {
                System.out.println("++++ client says " + rcvdMsg + " ++++");
            }
        } while(false);

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Server Console Output:

starting Listener 11...
Service Function started !!
Listening on port: 2244
Accepted connections ( Socket[addr=/127.0.0.1,port=62994,localport=2244]):1

Client Console:

Creating Node socket...
Community
  • 1
  • 1
Korba
  • 435
  • 1
  • 4
  • 18

1 Answers1

-2

I would change the do { } while(false) to a do { } while(true) otherwise it make no sense to have a while loop that runs only once and exit, i.e. the do-while(false) loop acts just as if it is not there.

  • Edited question to reflect there are two parallel threads 1. Listener (uses while(true) 2. Server thread (that reads/writes) on accepted connection sockets – Korba Mar 22 '17 at 18:01
  • Yes, i can and I should do that. However that still will not address the problem I am facing in creating ObjectInputStream object. – Korba Mar 22 '17 at 18:34
  • if you debug where is it hangeng up exactly , on which line, and it would be good to check the server ip and port on the client – Wissam Al-Wakeel Mar 22 '17 at 23:14