linux - socket programming with getaddrinfo -


i'm using getaddrinfo in socket programming in linux. have created client , server. client has hardcoded static port number. works fine.

but want system dynamically assign port number client whenever connects server. how do using getaddrinfo? i'm using tcp socket.

just don't call bind before calling connect , tcp stack assign client "random" source port. if need know port you're connecting (and don't), can call getsockaddr after calling connect.

alternatively, can call bind specifying port 0. in case, again, stack assign client "random" unused source port connect from. option preferred if don't want special case allowing implementation select port or if need specify local ip address reason.


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -