//============================================== file = udpClientSelect.c ===== //= A message "client" program to demonstrate sockets programming = //= - This is udpClient.c modified to use select() as a timer = //============================================================================= //= Notes: = //= 1) This program conditionally compiles for Winsock and BSD sockets. = //= Set the initial #define to WIN or BSD as appropriate. = //= 2) This program needs udpServer to be running on another host. = //= Program udpServer must be started first. = //= 3) This program assumes that the IP address of the host running = //= udpServer is defined in "#define IP_ADDR" = //= 4) To test this program (i.e., to demonstrate that the select() is = //= working) the program will print "Time Out every 5 seconds if not = //= receving anything from the server. = //=---------------------------------------------------------------------------= //= Example execution: (udpServer and udpClientSelect on host 127.0.0.1 = //= with delay added in udpServer before sendto()) = //= Time out of recvfrom()... = //= Time out of recvfrom()... = //= Time out of recvfrom()... = //= Time out of recvfrom()... = //= Time out of recvfrom()... = //= Time out of recvfrom()... = //= Received from server: This is a reply message from SERVER to CLIENT = //=---------------------------------------------------------------------------= //= Build: bcc32 udpClientSelect.c or cl udpClientSelect.c wsock32.lib = //= for Winsock, gcc udpClientSelect.c -lsocket -lnsl for BSD = //=---------------------------------------------------------------------------= //= Execute: udpClientSelect = //=---------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=---------------------------------------------------------------------------= //= History: KJC (10/24/10) - Genesis (from udpClient.c) = //= BT (09/21/14) - Made it to work for BSD = //= AA (11/04/17) - Added select to simulate timeout = //= KJC (11/05/14) - Minor clean-up = //= KJC (12/13/23) - Fix to select() for BSF (from Jo Embedded) = //============================================================================= #define WIN // WIN for Winsock and BSD for BSD sockets //----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #ifdef WIN #include // Needed for all Winsock stuff #endif #ifdef BSD #include #include #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for sockets stuff #include // Needed for select() #endif //----- Defines --------------------------------------------------------------- #define PORT_NUM 1050 // Port number used #define IP_ADDR "127.0.0.1" // IP address of server (*** HARDWIRED ***) //===== Main program ========================================================== #ifdef WIN void main(void) #endif #ifdef BSD int main(void) #endif { #ifdef WIN WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions #endif int client_s; // Client socket descriptor unsigned long int noBlock; // Non-blocking flag struct sockaddr_in server_addr; // Server Internet address struct timeval timeout; // Struct for time interval for select() fd_set recvsds; // Recieve descriptor for select() int addr_len; // Internet address length char out_buf[4096]; // Output buffer for data char in_buf[4096]; // Input buffer for data int retcode; // Return code int s_socket; // Retern code for select() #ifdef WIN // This stuff initializes winsock WSAStartup(wVersionRequested, &wsaData); #endif // Create a socket client_s = socket(AF_INET, SOCK_DGRAM, 0); if (client_s < 0) { printf("*** ERROR - socket() failed \n"); exit(-1); } // Fill-in server socket's address information server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_port = htons(PORT_NUM); // Port num to use server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use // Assign a message to buffer out_buf strcpy(out_buf, "Test message from CLIENT to SERVER"); // Now send the message to server. retcode = sendto(client_s, out_buf, (strlen(out_buf) + 1), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (retcode < 0) { printf("*** ERROR - sendto() failed \n"); exit(-1); } // Uses select() to make the recvfrom() time out while (1) { // Clearing and setting the recieve descriptor FD_ZERO(&recvsds); FD_SET((unsigned int) client_s, &recvsds); // Setting the time interval to 1 second timeout.tv_sec = 1; timeout.tv_usec = 0; // Calling select() #ifdef WIN s_socket = select(1, &recvsds, NULL, NULL, &timeout); #endif #ifdef BSD s_socket = select(client_s+1, &recvsds, NULL, NULL, &timeout); #endif if (s_socket == 0) { printf("Time out of recvfrom()... \n"); } else { addr_len = sizeof(server_addr); retcode = recvfrom(client_s, in_buf, sizeof(in_buf), 0, (struct sockaddr *)&server_addr, &addr_len); if (retcode < 0) { printf("*** ERROR - recvfrom() failed \n"); exit(-1); } // Output the received message printf("Received from server: %s \n", in_buf); // Break out of the loop break; } } // Close all open sockets #ifdef WIN retcode = closesocket(client_s); if (retcode < 0) { printf("*** ERROR - closesocket() failed \n"); exit(-1); } #endif #ifdef BSD retcode = close(client_s); if (retcode < 0) { printf("*** ERROR - close() failed \n"); exit(-1); } #endif #ifdef WIN // This stuff cleans-up winsock WSACleanup(); #endif }