Saving received data from CAsyncSocket to a queue

Hi All,
I'm using CASynchSocket in my application for networking using tcp/udp protocols.
I use a class named
MySocketwhich extends CAsynchSocket. It has a queue in which the received data on the OnReceive overried function is stored, this queue is accessed from outside MySocket to read/delete data.
MySocket has a function member IsDataAvailable which simply returns !queue.empty()
When running my application, I set a breakpoint in the OnReceive function, the data is received and stored in the queue, but if ask the
MySocket class about the availability of the data, the answer is NO. I set a breakpoint in theIsDataAvailable(), thequeue is empty even though OnReceived function is called before theIsDataAvailable() memeber function:-o here is a snapshot of my code:

class MySocket : public CAsyncSocket {
private:
deque <NetworkMsg> receivedData;
// Used to control access to receivedData structure.
CMutex queueProtector;

public:
ComSocket(){};
virtual ~ComSocket() {};
void Close() {CAsyncSocket::Close();};
BOOL IsOpen() const {return (m_hSocket != INVALID_SOCKET);};

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUDPSocket)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL

// Generated message map functions
//{{AFX_MSG(CUDPSocket)
// NOTE - the ClassWizard will add and remove member functions here.
virtual void OnSend(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnAccept(int nErrorCode);
//}}AFX_MSG
bool IsDataAvailable(void){
return !receivedData.empty()
};
NetworkMsg GetNextReceivedData(void);
};



Has any one of you already experienced this kind of problem?

And here is the OnReceive function

CString IPAddress;
char buffer[1024]; // Increase size as needed
UINT Port;
int Size;
CAsyncSocket::OnReceive(nErrorCode);
if (nErrorCode) {
printf("\nOnReceive nErrorCode: %i", nErrorCode);
return;
}
Size = ReceiveFrom(buffer, sizeof buffer, IPAddress, Port);
if (!Size || Size == SOCKET_ERROR) {
int err=GetLastError();
printf("\nReceiveFrom error code: %u", GetLastError());
return;
}
NetworkMsg nMsg;
nMsg.data = new char[Size];
nMsg.dataSize = Size;
for(int i=0; i< Size; i++){
nMsg.data[i] = buffer[i];
}
int i = this ->m_hSocket;
CSingleLock lock( &queueProtector );
lock.Lock();
receivedData.push_back(nMsg);
lock.Unlock();


[3659 byte] By [GET@h] at [2008-2-15]
# 1
Here is the code I use to send the data over the network
Code Snippet
char discoveryMsg[] = {'S', 'E', 6, 1, 2, 0};
UDPSend(discoveryMsg,"255.255.255.255", UDP_PORT);
if(!sockets[0].IsOpen()){
return;
}
if(sockets[0].IsDataAvailable()){
NetworkMsg* msg= sockets[0].GetNextReceivedData();
return;
}

I was wrong on my first post, the IsDataAvailable() is called before OnReceive. I put a Sleep() right after the UDPSend(...) function to delay the main thread but, nothing is changed, still get nothinng.
I tried to use events, I block my main thread with a WaitForSingleObject, and set the event after receiving data but the application blocks undefinitly, does that mean that OnReceive callback is executed by the main thread? or did I missunderstand how ASynchSocket works...

Thanks for your help,

GET@h at 2007-9-28 > top of Msdn Tech,Windows Networking Development,Peer-to-Peer Networking...
# 2

This is the Peer-to-Peer Networking forum. Questions about Winsock and MFC should be directed to the Windows Networking Development Newsgroup -- http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.win32.programmer.networks.

Without seeing your code in which you've added the WaitForSingleObject() I can't tell if you've made any mistakes. Are you listening for UDP packets or are you just broadcasting them? Your test for IsOpen() only checks if the socket is invalid. It does not check if the socket is listening so it is quite possible that you aren't listening for UDP packets and therefore aren't receiving your own broadcasts.

EliotFlannery-MSFT at 2007-9-28 > top of Msdn Tech,Windows Networking Development,Peer-to-Peer Networking...
# 3

Hi,

Thanks for the answer, I initially posted this problem in the Visual C++ faq since I have trouble using an MFC object but it has been moved here.

According to the documentation and the examples that I found, CAsyncSocket is listening after it has been created (in case of UDP) so there is no need to call the Listen function member to listen to UDP activity, that's all related to the nature of UDP.

Regards,

GET@h at 2007-9-28 > top of Msdn Tech,Windows Networking Development,Peer-to-Peer Networking...
# 4

Did someone on the Visual C++ forum tell you to move your question to the Peer-to-Peer Networking Developers forum? If so they didn't give very good advice. A better venue for your question is the microsoft.public.win32.programmer.networks newsgroup (http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.win32.programmer.networks). That is where Winsock and other general networking questions should be asked.

I do not know how CAsyncSocket works. Please post your source code and question on the general networking developer newsgroup (accessible via an http interface linked above). You will be able to get help there.

-Eliot

EliotFlannery-MSFT at 2007-9-28 > top of Msdn Tech,Windows Networking Development,Peer-to-Peer Networking...