當我運行下面的代碼:如何讓內核選擇在TCP套接字編程的範圍內(1024,5000)端口號
struct sockaddr_in sin;
int addrlen;
addrlen=sizeof(sin);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr=inet_addr("123.456.789.112");
sin.sin_port=htons(0); // so that the kernel reserves a unique port for us
sd_server = socket(PF_INET, SOCK_STREAM, 0);
bind(sd_server, (struct sockaddr *) &sin, sizeof(sin));
getsockname(sd_server,(struct sockaddr*)&sin,&addrlen);
port=ntohs(sin.sin_port);
printf("port number = %d\n",port);
據插座,我得和1024之間的端口號5000,但我得到的端口號大約是30,000。
我該怎麼辦?
C POSIX API,從你的代碼中推導出來嗎? Python:http://stackoverflow.com/questions/1365265/on-localhost-how-to-pick-a-free-port-number || Java:http://stackoverflow.com/questions/2675362/how-to-find-an-available-port ||殼:http://unix.stackexchange.com/questions/55913/whats-the-easiest-way-to-find-an-unused-local-port – 2015-12-19 15:28:10