//================================================= file = udpErrorDemo.c ===== //= A sockets program to demonstrate WSAGetLastError() for Winsock = //============================================================================= //= Notes: = //= 1) This program compiles only for Winsock = //= 2) This program does an incorrect socket call and then returns an = //= error. = //= 3) Socket error codes can be found on MSDN: = //= - http://msdn.microsoft.com/en-us/library/windows/desktop/ = //= ms740668(v=vs.85).aspx = //=---------------------------------------------------------------------------= //= Example execution: = //= *** ERROR - bind() failed with WSA error = 10038 = //=---------------------------------------------------------------------------= //= Build: bcc32 udpErrorDemo.c or cl udpErrorDemo.c wsock32.lib for Winsock = //=---------------------------------------------------------------------------= //= Execute: udpErrorDemo = //=---------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=---------------------------------------------------------------------------= //= History: KJC (09/19/14) - Genesis (from udpServer.c) = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #include // Needed for exit() #include // Needed for all Winsock stuff //----- Defines --------------------------------------------------------------- #define PORT_NUM 1050 // Arbitrary port number for the server //===== Main program ========================================================== int main() { WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions int server_s; // Server socket descriptor struct sockaddr_in server_addr; // Server Internet address struct sockaddr_in client_addr; // Client Internet address struct in_addr client_ip_addr; // Client IP address int addr_len; // Internet address length int retcode; // Return code // This stuff initializes winsock WSAStartup(wVersionRequested, &wsaData); // Create a socket server_s = socket(AF_INET, SOCK_DGRAM, 0); if (server_s < 0) { printf("*** ERROR - socket() failed with WSA error = %d \n", WSAGetLastError()); exit(-1); } // Create an error condition... server_s = 0; // Fill-in my socket's address information server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_port = htons(PORT_NUM); // Port number to use server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address retcode = bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (retcode < 0) { printf("*** ERROR - bind() failed with WSA error = %d \n", WSAGetLastError()); exit(-1); } // Close all open sockets retcode = closesocket(server_s); if (retcode < 0) { printf("*** ERROR - closesocket() failed with WSA error = %d \n", WSAGetLastError()); exit(-1); } // This stuff cleans-up winsock WSACleanup(); // Return zero and terminate return(0); }