Click to See Complete Forum and Search --> : i'm new programmer, plz help me in DATAGRAM


zagaa
October 11th, 2005, 06:58 AM
i make client server application in DATAGRAM. but if server waiting data to recieve cannot send data. it cannot work like thread. i know "recvfrom" is waiting data comming. but i cannot check data is coming or not. i dont know how i check it. i readed in "CAsyncSocket" it have "onRecieve" function can be used. but in here how i can?. plz help me. thanks

zagaa


int CUDPServer::Run()
{
SOCKET sockfd;
int n;
SOCKADDR_IN servaddr, cliaddr;
char mesg[100];
int sin_size = sizeof(struct sockaddr_in);
int mBind;
DWORD mRet = 0x01L;

if (ConInitialize() == FALSE)
return 0;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
memset(&(servaddr.sin_zero), 0, 8);

mBind = bind(sockfd, (LPSOCKADDR) &servaddr, sizeof(servaddr));

while(1)
{
if (mSendBool == FALSE && mRecvBool == FALSE)
Sleep(1);

// transiver
if (mSendBool == TRUE)
{
strcpy(mesg, mSendStr);
sendto(sockfd, mesg, strlen(mesg), 0, (LPSOCKADDR) &cliaddr, sin_size);
mSendBool = FALSE;
}
// reciever
//mRet = WaitForMultipleObjects(sockfd, 50);
if (mRecv == TRUE)
{
n = recvfrom(sockfd, mesg, 100, 0,(LPSOCKADDR) &cliaddr, &sin_size);
if (n > 0)
{
mesg[n] = '\0';
mRecvStr.Format("%s", mesg);
mRecvBool = FALSE;
}
}
}
return CWinThread::Run();
}

Richard.J
October 11th, 2005, 09:56 AM
inside your while-loop, use select() with a timeout. That way, you can also get rid of the Sleep().

if select() returns, either there is some data to be received or the timeout occurred.

HTH,
Richard

zagaa
October 12th, 2005, 01:19 AM
Thank you i used select. it works good. Thank you agian.

sincerely
zagaa