简体   繁体   English

如果服务器未读取,则跳过Java客户端套接字写入

[英]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. 我有一个Java程序,客户端将使用套接字不断地向服务器连续发送一个数字。 Usually the 'readObject' or 'writeObject' calls will sort of wait until the other side is ready to send or receive. 通常,“ readObject”或“ writeObject”调用将等待,直到另一端准备好发送或接收为止。 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: 我有一个Request类来发送信息,对于我来说,这只是一个整数:

    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. 因此,这里的事情是,我的客户端发送一个Request对象,并将类型设置为ADD。 The server will read/receive the Request, and check the type, and find that it is ADD, so it will call 'serv.add'. 服务器将读取/接收请求,并检查类型,并发现它是ADD,因此它将调用“ serv.add”。 This call may cause a "wait()" call, which means the Thread here is paused at this point, and, thus, stuck. 此调用可能会导致“ wait()”调用,这意味着此处的线程此时已暂停,因此被卡住了。 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. 客户端将尝试发送下一个Request对象,但是由于服务器线程卡在了“ serv.add”调用上,因此客户端似乎没有写任何内容,只是跳过了该调用,并进行了while循环再次。 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? 所以我想我的问题是:如果服务器当前未尝试从客户端读取,而客户端正在尝试写入,则'writeObject'调用会超时,然后继续,还是发生了什么?

So for future reference. 因此,以供将来参考。 The writeObject will just send a bunch of objects to the server. writeObject只会将一堆对象发送到服务器。 The readObject will read them at its own pace. readObject将按照自己的进度读取它们。 There seems to be some kind of internal buffer in relation to the readObject. 似乎有一些与readObject相关的内部缓冲区。 My client sends 30 integers, the server reads 10, then wait()s. 我的客户端发送30个整数,服务器读取10个整数,然后是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. 所以我什么都不丢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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