|
struct sockaddr_in { sa_family_t sin_family; /* Socket's address family - Must contain the value AF_INET */ in_port_t sin_port; /* Socket's Internet Port # - a 2 bytes port number */ struct in_addr sin_addr; /* Socket's Internet Address (IP-address) - a 4 bytes IP address */ }; |
Meaning of the variables:
|
|
(1) We first define a socket address variable: struct sockaddr_in in_addr; (2) Then we initialize the socket address: in_addr.sin_family = AF_INET; /* Protocol domain is: Internet */ in_addr.sin_addr.s_addr = ....; /* IP address in netw byte order */ in_addr.sin_port = ....; /* Port # in netw byte order */ |
(We will complete the example later after a discussion on byte ordering)
|
|
Example:
|
|
|
|
Program code:
/* =============================================== Define a socket address variable =============================================== */ struct sockaddr_in in_addr; /* ====================================================================== Initialize the socket address ====================================================================== */ in_addr.sin_family = AF_INET; /* Protocol domain is: Internet */ int IP = 170*256*256*256 + 140*256*256 + 150*256 + 1; // IP address is in host byte order !!! in_addr.sin_addr.s_addr = htonl(IP); /* IP address in network byte order */ in_addr.sin_port = htons(8000); /* Port # in network byte order */ |
|
|