/* Simple TCP socket connect test application. on ROS you can run this once. Running it twice causes error and BSOD crash saying multiple IRPs were recevied. NOTE: cleanup is carried out properly, this appears to be a winsock bug. */ #include #include #include /* for sleep() */ #include /* pragma comment(lib,"ws2_32.lib") //Winsock Library */ #define BUFLEN 512 //Max length of buffer #define PORT 8888 //The port on which to listen for incoming data int main() { SOCKET st, ct; // server udp, server tcp, client tcp and connection accepted tcp sockets struct sockaddr_in server;//, si_other; WSADATA wsa; char message[BUFLEN]; //Initialise winsock printf("\nTCP dynamic port Socket connection test app starting."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { printf("Winsock init Failed. Error Code : %d",WSAGetLastError()); exit(EXIT_FAILURE); } else { printf("Winsock Initialized on test App.\n"); //Create a TCP socket if((st = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) { printf("Could not create server socket : %d" , WSAGetLastError()); } else printf("TCP server side AF_INET Socket created succesfully.\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; // <<=== BIND TO ALL IP ADDRESSES server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons( 8888 ); //Bind if( bind(st ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR) { printf("server socket Bind on INADDR_ANY port 8888 failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } else { puts("TCP socket Bind INADDR_ANY port 8888 completed succesfully"); // start TCP socket listening if( listen(st ,SOMAXCONN) == SOCKET_ERROR) { printf("server socket Listen call failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } puts("TCP socket Listen done, the TCP port should now be visible in netstat"); puts("(press enter to continue after checking)"); gets(message); // now we create yet another TCP socket but specifying port zero to get a dynamic port number //Create a TCP socket if((ct = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) { printf("Could not create client side socket : %d" , WSAGetLastError()); } else { printf("TCP client Socket created.\n"); //Prepare the sockaddr_in structure for client socket server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); // <<=== source is loopback IP address server.sin_port = htons( 8889 ); // set to fixed port to see if it changes anything. port zero to get dynamic port assigned.. don't care //Bind if( bind(ct ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR) { printf("client socket Bind failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } puts("TCP client socket Bind completed using dynamic port "); //Prepare the target IP sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); // <<=== connect to loopback IP address server.sin_port = htons( 8888 ); // connect to our target port which is already listening on first socket if( connect(ct ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR) { printf("client socket Connect failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } else { puts("TCP client socket Connected successfully. "); } puts("TCP socket pair created/connected, calling closesocket for both. "); puts("closesocket/WSACleanup should free resources so port can be used again. "); if ( closesocket(ct) ) { printf("client socket closesocket failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } } if ( closesocket(st) ) { printf("server socket closesocket failed with error code : %d" , WSAGetLastError()); exit(EXIT_FAILURE); } } WSACleanup(); } puts("Winsock cleanup is completed, check port in netstat(should not be listed)"); puts("press enter to exit test. Run the application again to see BSOD."); gets(message); return 0; }