简体   繁体   中英

Waiting for reply in Socket Programming

I am opening and Socket connection and then I need to reply for the result from the server before moving forward.

While(true)
{
Receive message from Client
Process message.
Send query to client .
//wait for message
Receive the result from client.
}

But when the client replies, the message is received at step 1 but I need to receive it at step 4. I am not able to understand how to program it. I have basic server and client code ready. Can somebody just point me in the right direction.

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

Not very clear your architecture, but you'll always receive messages on first step. Maybe you need to use blocking call or another solution. Eg

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)

the message is received at step 1 but I need to receive it at step 4.

So do that.

I am not able to understand how to program it

Program step 4 the same way you programmed step 1.

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