// JP opened flex table

Click to See Complete Forum and Search --> : OnReceive udp


laasunde
February 25th, 2004, 07:46 AM
Need some help here. My OnReceive() methode doesnt seem to be working any longer. I've put a breakpoint in the method put it doesnt seem to execute. I use SendData() to try and trigger the event put nothing happens.

Would appreciate any help on how to debug this problem ? Im using the correct ip and portnr, also set FD_READ. I'm using udp socket so Listen() isnt needed.



//Header file
class CWinsocket: public CAsyncSocket
{
bool InitSocket(int port=0);
int SendData(char * Buffer, CString address, UINT port);
void OnReceive(int nErrorCode);
};


// Source code
bool CWinsocket::InitSocket(int port)
{
if (CAsyncSocket::Create(port, SOCK_DGRAM, FD_READ) == 0)
return false;

return true;
}

void CWinsocket::OnReceive(int nErrorCode)
{
char Buffer[1024];
int iReadBytes = CAsyncSocket::Receive((void*) Buffer, 1023);
CAsyncSocket::OnReceive(nErrorCode);
}

int CWinsocket::SendData(char * Buffer, CString address, UINT port)
{
int iSizeOfBuffer = 1024;
int iLength = CAsyncSocket::SendTo((void*) sendData, iSizeOfBuffer , port, (LPCTSTR) address);
delete Buffer;
if (iLength == SOCKET_ERROR || iLength != iSizeOfBuffer )
{
return -1;
}
return 0;
}

laasunde
February 25th, 2004, 12:58 PM
What could be a reason why OnReceve wont work properly ?

The socket is beeing create ok, according to "netstat -a" the socket is listening to the correct port number. Have set the DF_READ flag.

Sam Hobbs
February 25th, 2004, 01:57 PM
I have written only one program that uses sockets, but the essential parts of it are in my Receiving UDP Using CAsyncSocket (http://simplesamples.info/MFC/CAsyncSocketUDPReceive.php). Let me know if there are any problems with my code.

serup
February 26th, 2004, 07:46 AM
I had a similar problem with OnReceive() not being called, and in my case it was because the underlying socket system did not give any notifications. I solved this by having a thread reading with "recv(m_hSocket, (LPSTR)m_chInBuf, 1, MSG_PEEK);", all this is doing is to PEEK at the socket and telling the underlying socket system to make a notification when data comes.

This is not the best solution, but I have not been able to find a better one. If you do find one which does not use this "polling" approach, then please let me know.

Sincerely,
Johnny

Sam Hobbs
February 26th, 2004, 08:35 AM
Originally posted by serup
This is not the best solution, but I have not been able to find a better one. If you do find one which does not use this "polling" approach, then please let me know.Did you not see the sample I have?

Andreas Masur
February 26th, 2004, 10:47 AM
[Moved thread]

serup
February 26th, 2004, 10:59 AM
Originally posted by Sam Hobbs
Did you not see the sample I have?

So you have a non-polling example, sounds really interesting, finally some usefull information. Maybe you could re-write your hyperlink, since it does not work

Sam Hobbs
February 26th, 2004, 04:03 PM
Originally posted by serup
Maybe you could re-write your hyperlink, since it does not work It is now working.

//JP added flex table