UDP Select() TCP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    UDP Select() TCP

    hi. ok my problem is that i have been using TCP for a while and now i want to change to UDP. Im using the select I/0 method.

    i have a listening thread like this :
    Expand|Select|W rap|Line Numbers

    1. void Server::vListen ingThread( LPVOID pParam )
    2. {
    3. Server *pServer = (Server*)pParam ;
    4. SOCKET socketClient;
    5. do
    6. {
    7. socketClient = accept( *pServer->p_socketServer , 0, 0 );
    8.
    9. if ( socketClient == SOCKET_ERROR || pServer->usNumberOfClie nts >= pServer->usMAX_CLIENT S )
    10. {
    11. // send an error code to client //////////////////////////////////////
    12. closesocket( socketClient );
    13. }
    14. else
    15. {
    16. WaitForSingleOb ject( pServer->handleMutex, INFINITE );
    17. FD_SET( socketClient, &pServer->fdsetMaster );
    18. ReleaseMutex( pServer->handleMutex );
    19. pServer->usNumberOfClie nts++;
    20. }
    21.
    22. }while( pServer->bConnected );
    23.
    24. }



    now im unsure how to change it into UDP.

    does the new socket of data that has been sent go straight into the FD_SET? Therefore when im polling through the set i would have to check if it has been set or not?

    Please help, im a little bit confused with this?

    thanks
  • themadme
    New Member
    • Mar 2007
    • 53

    #2
    TCP version:

    Code:
    Server *pServer = (Server*)pParam;
    	SOCKET socketClient;
    	do
    	{
    		socketClient = accept( *pServer->p_socketServer, 0, 0 );
    
    		if ( socketClient == SOCKET_ERROR || pServer->usNumberOfClients >= pServer->usMAX_CLIENTS ) 
    		{
    			// send an error code to client //////////////////////////////////////
    			closesocket( socketClient );
    		}
    		else 
    		{
    			WaitForSingleObject( pServer->handleMutex, INFINITE );
    			FD_SET( socketClient, &pServer->fdsetMaster );
    			ReleaseMutex( pServer->handleMutex );
    			pServer->usNumberOfClients++;
    		}
    
    	}while( pServer->bConnected );
    and then later
    Code:
    int SelectResult = select( 0, &fdsetPolling, NULL, NULL, &timevalWait );
    
    			if ( SelectResult || SelectResult != SOCKET_ERROR ) 
    			{
    				for ( USHORT usLoop = 0; usLoop < fdsetPolling.fd_count; usLoop++ ) 
    				{
    					///** ok everythings is ok.  Now to get the socket with new data **//////
    					C_PacketData.vSetSocketData( fdsetPolling.fd_array[ usLoop ] );
    
    					///** hnow receiver the data from that socket **//////
    					int Retrieved = recv( tempSocket, (char*)&C_PacketData, pServer->uPACKET_SIZE, 0 );
    }
    }
    so for UDP i would i just delete the first part and for the second part i would add in this

    FS_ISSET( fdsetPolling.fd _array[ usLoop ], fdsetPolling )

    before i call C_PacketData.vS etSocketData( fdsetPolling.fd _array[ usLoop ] );

    correct me if im wrong or missing out something?

    thanks

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Sorry I've been holding off because on the whole I only use TCP sockets so I am not so familiar with UDP.

      I see nothing about the select(...) function that precludes using it on a UDP socket. Clear since UDP is connectionless you can not accept(...) a connection.

      A good place to start may be the Wikipedia article on Berkley sockets..

      Comment

      • themadme
        New Member
        • Mar 2007
        • 53

        #4
        thank you, much appreciated.

        Comment

        Working...