How to get ip address from sock structure in c?

后端 未结 3 1918
萌比男神i
萌比男神i 2020-12-02 20:06

I\'m writing simple server/client and trying to get client IP address and save it on server side to decide which client should get into critical section. I googled it severa

3条回答
  •  既然无缘
    2020-12-02 20:54

    The easier and correct way for extracting IP address and port number would be:

    printf("IP address is: %s\n", inet_ntoa(client_addr.sin_addr));
    printf("port is: %d\n", (int) ntohs(client_addr.sin_port));
    

    The SoapBox's accepted answer won't be correct for all architectures. See Big and Little Endian.

提交回复
热议问题