The basic structure used in building sockets is called
a sockaddr. The sockaddr looks like this:
struct sockaddr {
unsigned short sa_family; /*address family*/
char sa_data[14]; /*address data*/
};
The basic idea is to build a chunk of memory that holds all the critical information of
the socket, namely the type of address family used (in our case IP, Internet Protocol), the
IP address, and the port to be used. The last two elements are stored in the sa_data field.
To assist in referencing the fields of the structure, a more recent version of sockaddr
was developed: sockaddr_in. The sockaddr_in structure looks like this:
struct sockaddr_in {
short int sin_family /* Address family */
unsigned short int sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
unsigned char sin_zero[8]; /* 8 bytes of NULL padding for IP */
};
The first three fields of this structure must be defined by the user prior to establishing
a socket. We will be using an address family of 0x2, which corresponds to IP (network
byte order). Port number is simply the hex representation of the port used. The Internet
address is obtained by writing the octets of the IP (each in hex notation) in reverse order,
starting with the fourth octet.
Pages:
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425