What are Sockets?
Sockets provide a standard connection protocol which enables the communication of data over a network.Sockets can be easily configured to act like a client (The one which initiates the connection), or can act as a server (The one which accepts the connection).
The Socket Structures:
The first structure is struct sockaddr that holds socket information.
struct sockaddr
{
unsigned short sa_family; /* address family */
char sa_data[14]; /* 14 bytes of protocol address */
};
Attribute | Values | Description |
---|---|---|
sa_family | AF_INET AF_UNIX AF_NS AF_IMPLINK | This represents an address family. In most of the Internet based applications we use AF_INET. |
sa_data | Protocol Specific Address | The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family we will use port number IP address which is represented by sockaddr_in structure defined below. |
But, there's another structure (struct sockaddr_in) that help you to reference to the socket's elements.
struct sockaddr_in
{
short int sin_family; /* Address family */
unsigned short int sin_port; /* Port */
struct in_addr sin_addr; /* Internet Address */
unsigned char sin_zero[8]; /* Same size as struct sockaddr */
};
Attribute | Values | Description |
---|---|---|
sa_family | AF_INET AF_UNIX AF_NS AF_IMPLINK | This represents an address family. In most of the Internet based applications we use AF_INET. |
sin_port | Service Port | A 16 bit port number in Network Byte Order. |
sin_addr | IP Address | A 32 bit IP address in Network Byte Order. |
sin_zero | Not Used | You just set this value to NULL as this is not being used. |
struct in_addr
{
unsigned long s_addr;
};
Attribute | Values | Description |
---|---|---|
s_addr | service port | A 32 bit IP address in Network Byte Order. |
There is one more important structure. This structure is used to keep information related to host.
struct hostent
{
char * h_name; /* Official name of host. */
char ** h_aliases; /* Alias list. */
int h_addrtype; /* Host address type. */
int h_length; /* Length of address. */
char ** h_addr_list; /* List of addresses from name server. */
#define h_addr h_addr_list[0] /* Address, for backward compatibility. */
};
Attribute | Values | Description |
---|---|---|
h_name | ti.com etc | This is official name of the host. For example tutorialspoint.com, google.com etc. |
h_aliases | TI | This will hold a list of host name aliases. |
h_addrtype | AF_INET | This contains the address family and in case of Internet based application it will always be AF_INET |
h_length | 4 | This will hold the length of IP address which is 4 for Internet Address. |
h_addr_list | in_addr | For the Internet addresses the array of pointers h_addr_list[0], h_addr_list[1] and so on are points to structure in_addr. |
Following structure is used to keep information related to service and associated ports.
struct servent
{
char *s_name;
char **s_aliases;
int s_port;
char *s_proto;
};
Attribute | Values | Description |
---|---|---|
s_name | http | This is official name of the service. For example SMTP, FTP POP3 etc. |
s_aliases | ALIAS | This will hold list of service aliases. Most of the time this will be set to NULL. |
s_port | 80 | This will have associated port number. For example for HTTP this will be 80. |
s_proto | TCP UDP | This will be set to the protocol used. Internet services are provided using either TCP or UDP. |
You can also subscribe here and my warm welcome to my blog
ReplyDeleteknowledgeshareindia.blogspot.com