2014-04-02 56 views
0

我在打開和Socket連接,然後我需要在前進之前回復服務器的結果。在Socket編程中等待回覆

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

但是,當客戶端的應答,該消息在步驟1中接收,但我必須接受它在步驟4,我不能夠理解如何將它編程。我準備好了基本的服務器和客戶端代碼。有人能指出我正確的方向嗎?

回答

-1

消息在步驟1中收到的,但我需要在步驟4

接受它那麼做。

我無法理解如何編程它

計劃第4步你編程步驟1

+0

我可以,但步驟1是在不同的線程。讓線程繼續運行,並不斷接受消息。因此,來自步驟3的客戶端回覆在該線程上被接收。如果我想特別針對第3步的回覆。如果我的問題不明確,我很抱歉。 – Nikhil

+1

所以你不能這樣做。你已經把自己塗在角落裏了。 – EJP

+0

猜你是對的。將嘗試一種新方法。謝謝 – Nikhil

1
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) 
+0

我的消息有一個類型,但步驟1在另一個線程上。讓線程繼續運行,並不斷接受消息。因此,來自步驟3的客戶端回覆在該線程上被接收。如果我想特別針對第3步的回覆。如果我的問題不明確,我很抱歉。 – Nikhil

+0

我更新我的答案 – zella