Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Socket编程中的等待应答_Java_Sockets - Fatal编程技术网

Java Socket编程中的等待应答

Java Socket编程中的等待应答,java,sockets,Java,Sockets,我正在打开和套接字连接,然后我需要在继续之前回复服务器的结果 While(true) { Receive message from Client Process message. Send query to client . //wait for message Receive the result from client. } 但是当客户端回复时,在步骤1接收到消息,但我需要在步骤4接收它。我无法理解如何编程。我已经准备好了基本的服务器和客户端代码。谁能给我指一下正确的方向吗 消息在步骤1收

我正在打开和套接字连接,然后我需要在继续之前回复服务器的结果

While(true)
{
Receive message from Client
Process message.
Send query to client .
//wait for message
Receive the result from client.
}
但是当客户端回复时,在步骤1接收到消息,但我需要在步骤4接收它。我无法理解如何编程。我已经准备好了基本的服务器和客户端代码。谁能给我指一下正确的方向吗

消息在步骤1收到,但我需要在步骤4收到它

那就这么做吧

我无法理解如何编程

按照与步骤1相同的方式对步骤4进行编程

While(true)
{
//wait for message - you should receive message in one point
Receive message from Client
Process message.
if (message==1) Send query to client .
if (message==2) This is result from client

}
upd

不太清楚您的体系结构,但您总是在第一步收到消息。也许您需要使用阻塞呼叫或其他解决方案。乙二醇

3 step:
Send query to client.
wait() // blocking call - waiting result

1 step:
if (message==2) //This is result from client
pass reply to 3 step(another thread)

我可以,但是第一步是在另一个线程上。所以线程继续运行,并不断接受消息。因此,在该线程上接收到来自步骤3的客户端回复。如我所愿,此回复特别适用于第3步。如果我的问题不清楚,我很抱歉。所以你当时不能做。你把自己画进了一个角落。我想你是对的。我会尝试一种新的方法。ThanksMy消息有一个类型,但步骤1位于不同的线程上。所以线程继续运行,并不断接受消息。因此,在该线程上接收到来自步骤3的客户端回复。如我所愿,此回复特别适用于第3步。如果我的问题不清楚,我很抱歉。