sockaddr_in

Defines a TCP/IP network address that can be used to pass the address to the sockaddr structure.

Service:Networking

Structure

  #include <netinet/in.h>
  
  struct sockaddr_in 
  {
     short              sin_family; 
     u_short            sin_port; 
     struct in_addr     sin_addr; 
     char               sin_zero[8];   
  };
  

Fields

sin_family

Contains the address family and must be AF_INET.

sin_port

Corresponds to the TCP or UDP port number (in network byte order). If set to 0, the protocol assigns an unused, nonprivileged value.

sin_addr

Contains a 4-byte Internet address (in network byte order). If you do not know the address you want to bind to, set this field to 0.

sin_zero

Must be initialized to zero.

Remarks

For a TCP or UDP over IPv4 socket, initialize the sockaddr_in fields as follows:

  • sin_family should be AF_INET.

  • sin_port can be a 16-bit local port value in the domain (in network byte order). If it is 0, the protocol assigns an unused, nonprivileged value between 1024 and 5000 as the port number. Values 1-1023 and 5001-65535 are reserved for well-known server ports.

  • sin_addr can usually be set to 0 when specifying a local sockaddr structure. This allows the protocol to determine the appropriate local IP address (depending on the route). The client can set a local address explicitly (in network byte order) if the address belongs to the node. You use the in_addr structure to specify the address. A destination address requires a filled-in sockaddr structure.

  • sin_zero must be set to eight bytes of 0.

Each field in this structure must be initialized to a valid value.