Socket prblem
-
Hi all, i have one problem related to thread. i am having my main thread running which will on some event create a thread for validation purpose and then will start working. and now this validator thread will create a socket and it will bind to port 20111. and then will wait for client to connect. but before waiting for client to connect it will send a message to the main thread that validation is done. but in this way i need to stick with port 20111.which i really don't want. Is it possible for me to get a free port from operating system or from winsock library and then i should bind to this port?? and one more thing i want is i want to get the free port no that operating system has given in an variable because i will need that port number further in the main programs lifetime. the code is as follows UINT Thread_Func(LPVOID pParam) { wsaret = WSAStartup(0x101,&wsadata); if(wsaret != 0) return -1; sock.sin_family = AF_INET; sock.sin_addr.s_addr = INADDR_ANY; sock.sin_port = htons((unsigned short)20111);// iwant to obtain this port no on fly and // to get it in some variable so that i can use it further in my main thread server = socket(AF_INET,SOCK_STREAM,0); if(server == INVALID_SOCKET) return -1; if(bind(server,(SOCKADDR *)&sock,sizeof(sock))!=0) return -1; if(listen(server,1)!=0) return -1; cli_len = sizeof(cli); client = accept(server,(SOCKADDR *)&cli,&cli_len); if(client == 0) return -1; return 1; } i have not done socket programming before this is my first time i am doing it so if there are any mistakes please let me know Thanks and regards Harshal shete
-
Hi all, i have one problem related to thread. i am having my main thread running which will on some event create a thread for validation purpose and then will start working. and now this validator thread will create a socket and it will bind to port 20111. and then will wait for client to connect. but before waiting for client to connect it will send a message to the main thread that validation is done. but in this way i need to stick with port 20111.which i really don't want. Is it possible for me to get a free port from operating system or from winsock library and then i should bind to this port?? and one more thing i want is i want to get the free port no that operating system has given in an variable because i will need that port number further in the main programs lifetime. the code is as follows UINT Thread_Func(LPVOID pParam) { wsaret = WSAStartup(0x101,&wsadata); if(wsaret != 0) return -1; sock.sin_family = AF_INET; sock.sin_addr.s_addr = INADDR_ANY; sock.sin_port = htons((unsigned short)20111);// iwant to obtain this port no on fly and // to get it in some variable so that i can use it further in my main thread server = socket(AF_INET,SOCK_STREAM,0); if(server == INVALID_SOCKET) return -1; if(bind(server,(SOCKADDR *)&sock,sizeof(sock))!=0) return -1; if(listen(server,1)!=0) return -1; cli_len = sizeof(cli); client = accept(server,(SOCKADDR *)&cli,&cli_len); if(client == 0) return -1; return 1; } i have not done socket programming before this is my first time i am doing it so if there are any mistakes please let me know Thanks and regards Harshal shete
something in the back of my brain said: "sock.sin_port = 0" would let the OS select the port....
-
something in the back of my brain said: "sock.sin_port = 0" would let the OS select the port....
yes that is true but how i can access that port number??
-
yes that is true but how i can access that port number??
maybe you can reverse use the sock.sin_port after the connection has been occured with port = ntohs(sock.sin_port) from the socket.