Click to See Complete Forum and Search --> : How to synchronize the socket functions and user defined functions ?


prati
December 2nd, 2004, 12:11 AM
Hello!

Im Implementing an application with socket programming.I have a menu item when i click on item i get dialog box ,that dialog box i have called from the function which has been called from the selection of the menu item.

eg..
/////////////////////////////////////////////////////////////////////////////////////////

OnMenuItem()
{
CMyDialog dlg;
dlg.DoModal(); //some action to be performed in dialog

---------
-------- //wait till get response from socket // some action to be performed based on response.
}

/////////////////////////////////////////////////////////////////////////////////////////


In OnOK() function of the dialog, i have called another function say sockets send() function and then return back to the function from which the dialog box called and continues to that function.But here i want to wait till i get response from the socket,i have implemented receive() function for it which is independent function.
________________________________________________________________
PROBLEM :
_________
Problem is that when i returned to the calling function of the dlg.DoModal() it immidiately continues to the same function ,although i have sent the data to the server socket, then the receive function should get the response from the server socket independently , but when i put the break points to these functions i found it continues to the function first and then receives the response, which i don't want. So i have put Sleep(1000) function after dlg.DoModal() but nothing has happened. Receive() function never receives the response before returning from the function which has called dlg.DoModal().
________________________________________________________________

So please tell me the way how to synchronize the things so that i can receive the response from the socket at one hand and after that, continues to the function on the other hand.

Prati.

kuphryn
December 2nd, 2004, 12:24 AM
One solution is an event kernel object.

Kuphryn

prati
December 2nd, 2004, 12:40 AM
Hello sir,

Thanks for Your reply.

Will you please tell me, what is kernel object ? and how to solve the problem through it ?

prtai.

Mathew Joy
December 2nd, 2004, 12:41 PM
Check out the MSDN for these APIs. CreateEvent(), SetEvent() and WaitForSingleObject(). Synchronisation objects such as Events and Mutex, can be used to sychronise the code or vairable access such that when the object is in use/signaled the other has to wait till the oject is released/non-signaled. In your case you can use events to signal the arrival of data. Using Sleep() is a bad idea. Hope that helps.

MikeAThon
December 2nd, 2004, 06:14 PM
Do not wait on an event in the thread of your main GUI unless you can guarantee that the wait will be very short. Otherwise, your GUI will freeze (for example, the application will not paint itself) and your user will think that the application has crashed.

Are you using MFC sockets? If so, you might consider a separate thread for a blocking CSocket, or use of CAsyncSocket and AsyncSelect, followed by an override of the virtual OnReceive function.

Mike

prati
December 3rd, 2004, 06:01 AM
Hello Sir,
Thanks for reply,

Im using MFC sockets and also used CSocket and overriden the virtual OnReceive function ,and it also notifies when receives data from the server socket. but my problem is that it notifies me when the function returns(the function which calls the dialog box, i mentioned in my post).

Earlier i was thinking ,that because of network delay OnReceive function is not notifying ,that's why for checking purpose i put Sleep() function but after a long delay through Sleep() function i didn't find the reason of network delay behind this .It never notifies between the event handler function, only notifies the user after returning .

If there is a solution through SetEvent() then please tell me where to put this function.

Actually i have gone through MSDN for SetEvent() function but didn't understand properly,what i understood is .....CEvent object is a synchronization object that allows one thread to notify another that an event has occurred.But i have two types of event one is automatic event(OnReceive()) and other is user defined event........Now here i want to notify user defined event that an automatic event has occured.

So please tell me how to do it.

Prati.

MikeAThon
December 3rd, 2004, 11:07 AM
Im using MFC sockets and also used CSocket and overriden the virtual OnReceive function ,and it also notifies when receives data from the server socket. but my problem is that it notifies me when the function returns(the function which calls the dialog box, i mentioned in my post).

Earlier i was thinking ,that because of network delay OnReceive function is not notifying ,that's why for checking purpose i put Sleep() function but after a long delay through Sleep() function i didn't find the reason of network delay behind this .It never notifies between the event handler function, only notifies the user after returning .

I don't understand your problem. Your first paragraph says you are being notified, and your second paragraph says you are not.

Are you being notified or aren't you?

Is your question about the timing of when you are being notified?

Please clarify.

Mike

prati
December 3rd, 2004, 11:58 PM
I don't understand your problem. Your first paragraph says you are being notified, and your second paragraph says you are not.

sorry sir i think im not able to make you understand my problem properly instead im trying, please try to undersatnd.

Are you being notified or aren't you?
Yes im being notified.

Is your question about the timing of when you are being notified?

Yes my problem about the timing of when im being notified.


prati.

miteshpandey
December 9th, 2004, 03:37 PM
I think I understand your problem.

Your problem is because you u handle sockets in the same thread as that of the thread(primary thread) from which ur dialog box is shown. You will get OnReceive Notification only when the chain of functions that is produced by clicking the menu item and the DoModal function returns. I assume that when u call DoModal the primary thread enters some kind of loop inside the DoModal and therefore the thread cannot process other messages.

If I am correct this is a typical case of multithreading i.e. you require multithreading. If you handle socket in the primary thread handle the showing of the dialong in another thread. Or if you handle the dialog in the primary thread handle the sockets in secondary thread. Your other thread needs to be an UI thread.

Another solution which I found by chance is that don't use dialogs at all but instead use AfxMessageBox. If you use AfxMessageBox the same primary thread can handle socket notifications. How does AfxMessageBox implements this is beyond me wonder.

prati
December 10th, 2004, 01:00 AM
Hello,
Thanks a lot, for being understand my problem. you are right, you have interpreted my problem exactly the same that i wanted to make understandable by you people.

thank you,
prati.