简体   繁体   English

客户端while循环从服务器发送和接收

[英]Client while loop to send and receive from server

This is my client application that is connected to a server. 这是我连接到服务器的客户端应用程序。 I use an object called DataPackage to send information back and forth. 我使用一个称为DataPackage的对象来回发送信息。 The code runs in its own thread. 该代码在其自己的线程中运行。

The variable in is an ObjectInputStream and out is an ObjectOutputStream . 输入变量inObjectInputStreamout变量是ObjectOutputStream

DataPackage dp = null;
while (true) {

    try {
        dp = (DataPackage) in.readObject();
        if (dp != null) {
            // Do stuff with received object
        }

        if (sendSomething) { // Send stuff
            out.writeObject( new DataPackage("some data") );
            out.flush();
            out.reset();
        }
    } catch (ClassNotFoundException | IOException e1) {
        e1.printStackTrace();
    }

    try {
        t.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

The problem with my code is that I never reach the part where I send stuff, unless I receive something from the server. 我的代码的问题在于,除非从服务器接收到某些东西,否则我永远都不会到达发送东西的那一部分。 The loop waits for something from the server before it continues to if (sendSomething) . 循环会等待服务器发出的消息,然后继续执行if (sendSomething) How should I structure my code so that I can send stuff any time and receive stuff any time? 我应该如何构造我的代码,以便我可以随时发送和接收邮件? I hope you understand. 我希望你明白。

You can use the threads. 您可以使用线程。 If they use the same socket to communicate with an external server, this is common because they won't block while waiting to read from the socket. 如果他们使用相同的套接字与外部服务器进行通信,这是很常见的,因为它们在等待从套接字读取时不会阻塞。

Thread1: 线程1:

block on read on recieve message - do something 收到邮件时阻止阅读-做某事

Thread2: do things on something happening - write to socket 线程2:对正在发生的事情进行处理-写入套接字

The line dp = (DataPackage) in.readObject(); dp = (DataPackage) in.readObject(); is blocking. 正在阻止。 It means that it will not complete execution of lines below until it receives some reply from server. 这意味着,直到收到服务器的一些答复,它才能完成下面几行的执行。

One advise, create a new thread and pass instance of dp = (DataPackage) in.readObject(); 一个建议,创建一个新线程并在dp = (DataPackage) in.readObject();传递dp = (DataPackage) in.readObject(); so it won't block. 这样它就不会阻塞

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

相关问题 如何从客户端到服务器发送和接收多个数据 - How to send and receive multiple data from client to server 客户端和服务器在同一个应用程序中发送然后接收字节 - Client and Server in same app to send and then receive Bytes 更改客户端上的网络IP后,可以在C(服务器)中从Android(客户端)发送但无法接收 - Can send but not receive in C (server), from Android (client), after changing networks IP on client 我用netty编写了一个应用程序,将消息从客户端发送到服务器,但是服务器无法接收消息 - I write an application with netty, send message from client to server, but server cannot receive the message 如何使TCP客户端发送请求并接收响应。还接受服务器推送的消息 - How to make TCP client which send request and receive response .Also accepts the messages pushed from server 在服务器端接收值(reslet java)从客户端(php)发送 - Receive value in server side( reslet java ) send from client side (php ) 将多张照片从服务器发送到客户端卡在一个while循环中 - Sending multiple photos from server to client stuck in a while loop 无法从服务器到客户端接收语言文本 - Unable to receive language text from server to client 服务器没有收到来自客户端的请求 - Server doesn"t receive a request from the client 如何从服务器中的客户端接收输入? - How to receive input from client in server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM