c++ - Generation of unique IP-port using bind(0) -


I need to generate a port number for some programmers. Google says that by binding with zero-port Unused port bines. I want to pair zero port, remember that this is the number, the port closed. Trying to do this? But NetState does not see the port of my program. Linux 2.6.27

  int sfd; Struct sockaddr_in my_addr; Sfd = Socket (PF_INET, SOCK_STREAM, 0); If (sfd == -1) {printf ("socket error \ n"); Return -1; } Mementat (& amp; my_addr, 0, sizeof (struct sockaddr_in)); My_addr.sin_family = AF_INET; My_addr.sin_addr.s_addr = INADDR_ANY; My_addr.sin_port = 0; If (dam (SFD, (straight sockaddr_in *) & my_addr, sizeof (struct sockaddr_in)) == -1 printf ("bind error \ n"); If (listen (sfd, LISTEN_BACKLOG) == -1) printf ("hear error \ n");    

dam () a sockaddr * a sockaddr_in * :

  bind (sfd, (struct sockaddr *) and my_addr, sizeof (my_addr))   < p> In addition to this, I do not see any other error. Binding for port 0 is the correct way to bind a randomly available port. If  dam ()  and  listen ()  do not report a failure, then netstat should see an open port to find out which port  to bind ()  has really picked up, eg:  
  int sfd = socket (PF_INET, SOCK_STREAM, out to the  Vokkam (to)  0); If (sfd == -1) {printf ("socket error:% d \ n", errno); Return -1; } Struct sockaddr_in my_addr; Memc (& amp; my_addr, 0, size (my_addr)); My_addr.sin_family = AF_INET; My_addr.sin_addr.s_addr = INADDR_ANY; My_addr.sin_port = 0; If (dam (SFD, (Structure Sokadar *) and My_ADR), myfile (my_addr) == -1) {printf ("Tie error:% d \ n", errno); Close (SFD); Return -1; } If (listen (SFD, LISTEN_BACKLOG) == -1) {printf ("Listen Error:% d \ n", errno); Close (SFD); Return -1; } Mementat (& amp; my_addr, 0, sizeof (my_addr)); Socklen_t my_addrlen = sizeof (my_addr); If (Hokkenam (SFD (structure Sokad * * & amp; my_addr, & amp; my_addrlen) == -1) {printf ( "Vilsknam error:% d \ n", errno); close (SFD); return -1 ;} printf ("listen on port:% hoo \ n", ntohs (my_addr.sin_port));    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -