By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,944 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,944 IT Pros & Developers. It's quick & easy.

UDP Select() TCP

P: 53
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|Wrap|Line Numbers

1. void Server::vListeningThread( 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->usNumberOfClients >= pServer->usMAX_CLIENTS )
10. {
11. // send an error code to client //////////////////////////////////////
12. closesocket( socketClient );
13. }
14. else
15. {
16. WaitForSingleObject( pServer->handleMutex, INFINITE );
17. FD_SET( socketClient, &pServer->fdsetMaster );
18. ReleaseMutex( pServer->handleMutex );
19. pServer->usNumberOfClients++;
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
Nov 9 '08 #1
Share this Question
Share on Google+
3 Replies


P: 53
TCP version:

Expand|Select|Wrap|Line Numbers
  1. Server *pServer = (Server*)pParam;
  2.     SOCKET socketClient;
  3.     do
  4.     {
  5.         socketClient = accept( *pServer->p_socketServer, 0, 0 );
  6.  
  7.         if ( socketClient == SOCKET_ERROR || pServer->usNumberOfClients >= pServer->usMAX_CLIENTS ) 
  8.         {
  9.             // send an error code to client //////////////////////////////////////
  10.             closesocket( socketClient );
  11.         }
  12.         else 
  13.         {
  14.             WaitForSingleObject( pServer->handleMutex, INFINITE );
  15.             FD_SET( socketClient, &pServer->fdsetMaster );
  16.             ReleaseMutex( pServer->handleMutex );
  17.             pServer->usNumberOfClients++;
  18.         }
  19.  
  20.     }while( pServer->bConnected );
and then later
Expand|Select|Wrap|Line Numbers
  1. int SelectResult = select( 0, &fdsetPolling, NULL, NULL, &timevalWait );
  2.  
  3.             if ( SelectResult || SelectResult != SOCKET_ERROR ) 
  4.             {
  5.                 for ( USHORT usLoop = 0; usLoop < fdsetPolling.fd_count; usLoop++ ) 
  6.                 {
  7.                     ///** ok everythings is ok.  Now to get the socket with new data **//////
  8.                     C_PacketData.vSetSocketData( fdsetPolling.fd_array[ usLoop ] );
  9.  
  10.                     ///** hnow receiver the data from that socket **//////
  11.                     int Retrieved = recv( tempSocket, (char*)&C_PacketData, pServer->uPACKET_SIZE, 0 );
  12. }
  13. }
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.vSetSocketData( fdsetPolling.fd_array[ usLoop ] );

correct me if im wrong or missing out something?

thanks
Nov 10 '08 #2

Banfa
Expert Mod 5K+
P: 8,916
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..
Nov 10 '08 #3

P: 53
thank you, much appreciated.
Nov 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.