How can I obtain the local TCP port and IP Address of my client program?

后端 未结 2 708
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:49

I\'m prepping for a simple work project and am trying to familiarize myself with the basics of socket programming in a Unix dev environment. At this point, I have some basic

相关标签:
2条回答
  • 2021-01-13 16:22

    From the looks of your code you are not doing connect or accept on the socket. Until you have done this the data you get from getsockname is undefined.

    0 讨论(0)
  • 2021-01-13 16:33

    I don't think you can meaningfully call getsockname in this context until you've called connect() - the kernel doesn't bind it to a particular port until it needs to (or is told to do so explicitly), and the local address is chosen based on the routing table, and hence the destination.

    0 讨论(0)
提交回复
热议问题