I set the server.sin_port=0 and then I call the bind function that doesn't return any error:
Where can I get the port that was asigned by the bind function?
Code:int sd; //server descriptor struct sockaddr_in server; bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(0); if(bind(sd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1); // error printf("%d\n",ntohs(server.sin_port)); //is still 0