CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    4

    Post i'm new programmer, plz help me in DATAGRAM

    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

    Code:
    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();
    }
    Last edited by zagaa; October 11th, 2005 at 07:00 AM.

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: i'm new programmer, plz help me in DATAGRAM

    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

  3. #3
    Join Date
    Oct 2005
    Posts
    4

    Thank you

    Thank you i used select. it works good. Thank you agian.

    sincerely
    zagaa

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured