#include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { unsigned short out_port = 35000; char * endptr; struct sockaddr_in server_address; int out_socket; int i=1; if( argc > 1 ) { out_port = (unsigned short) strtol(argv[1], &endptr, 10); errno = 0; if(errno != 0 || out_port == 0 || endptr == argv[1] ) { perror("Invalid port number"); exit(1); } } printf( "Out port : %hu\n", out_port ); fflush(NULL); memset( &server_address, 0, sizeof(struct sockaddr_in) ); server_address.sin_family = AF_INET; server_address.sin_port = htons(out_port); if ( inet_pton( AF_INET, "127.0.0.1", &(server_address.sin_addr) ) <= 0 ) { perror("inet_pton error"); exit(1); } if ( (out_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket error"); exit(1); } while (connect(out_socket, (struct sockaddr *) &server_address, sizeof(struct sockaddr_in)) < 0) { fprintf(stderr,"connect try %i failed : ",i); perror(""); fflush(NULL); i++; } printf("Connection success : %i try\n",i); fflush(NULL); if( close(out_socket) < 0 ) { perror("close error"); exit(1); } printf("Connection closed\n"); fflush(NULL); fprintf(stderr,"last error : "); perror(""); return 0; }