简体   繁体   中英

Java Socket - Inverse Communication

I have many clients that are waiting for server messages. So the client make accept() and wait for server. When server have messages, open a connection to the client and send messages, after that, close the communication and the cycle restart.

I've seen usually the inverse approach, where the server do accept() and client connect to it. I've wrote this code but the client (that do accept() ) is blocked on point 3 and the server (that create the connection to the client) is blocked on point 2 .

Sure i have some problems in my code (dont know where), but... this is the correct way ?

The client (that do accept() and wait for new messages)

try {
                System.out.println("Waiting..");
                receiver = serverSocket.accept();

                System.out.println("1");
                ObjectInput fromServerReader = new ObjectInputStream(receiver.getInputStream());
                ObjectOutputStream toServerWriter = new ObjectOutputStream(receiver.getOutputStream());
                System.out.println("2");
                toServerWriter.writeObject("dummy");
                toServerWriter.flush();
                System.out.println("3");

                ScheduledEvent scheduledEvent = (ScheduledEvent) fromServerReader.readObject();

                System.out.println("4");

                receiver.close();
                System.out.println("5");

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // Should never happen
            }

The server (that when have new message to send to client, create the connection)

try {
            InetAddress address = InetAddress.getByName(sendToUser
                    .getMachineName());
            socket = new Socket(address, port);

            log.debug("1");
            ObjectOutputStream toClientWriter = new ObjectOutputStream(
                    socket.getOutputStream());
            ObjectInputStream fromClientReader = new ObjectInputStream(socket.getInputStream());
            log.debug("2");

            String read = (String)fromClientReader.readObject();
            log.debug("3");

            // Compose the message
            ScheduledEvent scheduledEvent = new ScheduledEvent();
            scheduledEvent.setSubject(event.getSubject());
            scheduledEvent.setMessage(event.getText());

            log.debug("4");
            toClientWriter.writeObject(scheduledEvent);
            toClientWriter.flush();
            log.debug("5");

            socket.close();
            log.debug("6");
        } catch (UnknownHostException e) {
            // TODO handle
            e.printStackTrace();
        } catch (IOException | ClassNotFoundException e) {
            // TODO handle
            e.printStackTrace();
        }

In client code, instead of using

PrintWriter writer;

Use

ObjectOutputStream writer;

And then use

writer.writeObject("dummy");
writer.flush();

Try using println instead of write toServerWriter.println("dummy"); . The server may be waiting for the newline character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM