简体   繁体   中英

Java client socket writing is skipped if server is not reading

I have a java program, where a client will continuously send a number to a server, over and over, using sockets. Usually the 'readObject' or 'writeObject' calls will sort of wait until the other side is ready to send or receive. But in my specific case here, the client will just skip over the writing to server, if the server is not currently reading.

Below is my client code. I have a Request class to send the information, which is just an integer in my case:

    Socket socket = new Socket("localhost", 2910);
    ObjectOutputStream outToServer = new ObjectOutputStream(socket.getOutputStream());
    Random r = new Random();
    while(true) {
        Request request = new Request();
        request.reqType = Request.TYPE.ADD;
        request.add = r.nextInt(100);
        System.out.println("Sending to server: " + request.add);
        outToServer.writeObject(request);
        Thread.sleep(100);
    }

And here's the server part:

try {
        ObjectInputStream inFromClient = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream outToClient = new ObjectOutputStream(socket.getOutputStream());

        while(true) {
            Request req = (Request)inFromClient.readObject();

            switch (req.reqType) {
                case ADD : {
                    Logger.getInstance().log(name + " added " + req.add);
                    serv.add(req.add);
                    break;
                }
                case RESULT : {
                    Logger.getInstance().log(name + " produced result " + req.result);
                    break;
                }
                case RETRIEVE : {
                    int[] retrieve = serv.retrieve();
                    Logger.getInstance().log(name + " retrieved: " + retrieve[0] + ", " + retrieve[1] + ", " + retrieve[2]);
                    outToClient.writeObject(new Request(retrieve));
                    break;
                }
            }

        }
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

The above code is run in a Thread. So the thing here is, my client send a Request object, and set the type to ADD. The server will read/receive the Request, and check the type, and find that it is ADD, so it will call 'serv.add'. This call may cause a "wait()" call, which means the Thread here is paused at this point, and, thus, stuck. The client will attempt to send the next Request object, but because the server Thread is stuck on the 'serv.add' call, it appears the client doesn't write anything, and just skips the call, and does the while-loop over again. So I'm attempting to send a bunch of Requests, which the server doesn't receive.

I hope it makes sense.

So I guess my question is: If the server is not currently attempting to read from the client, and the client is attempting to write, will the 'writeObject' call sort of time out, and then just continue, or what's going on?

So for future reference. The writeObject will just send a bunch of objects to the server. The readObject will read them at its own pace. There seems to be some kind of internal buffer in relation to the readObject. My client sends 30 integers, the server reads 10, then wait()s. The client terminates. The server will at some point continue, and then read the rest of the sent objects. So I don't loose anything.

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