Interview Questions

How to Put a Host Program Address Together?

Beginners Guide to Sockets


(Continued from previous question...)

How to Put a Host Program Address Together?

Once a host address and port number are known then the complete process address must be put together in a form that may be used by the system calls already covered. The structures set up to allow this follow:

#include &ltnetinet/in.h>

/*
 * Internet address 
 (a structure for historical reasons)
 */
struct in_addr {
    union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
  struct { u_short s_w1,s_w2; } S_un_w;
         u_long S_addr;
    } S_un;
#define s_addr  S_un.S_addr
/* can be used for most tcp & ip code */
};


/*
 * Socket address, internet style.
 */
struct sockaddr_in {
      short   sin_family;
      u_short sin_port;
      struct  in_addr sin_addr;
      char    sin_zero[8];
};

Filling in the fields for sockaddr_in will produce an Internet version of a socket address.

(Continued on next question...)

Other Interview Questions