Tutorials Point


  Learning Unix Socket
  Socket Quick Guide
  Socket Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Socket Structures


previous next AddThis Social Bookmark Button


Advertisements

There are various structures which are used in Unix Socket Programming to hold information about the address and port and other information. Most socket functions require a pointer to a socket address structure as an argument. Structures defined in this tutorial are related to Internet Protocol Family.

The first structure is struct sockaddr that holds socket information:

struct sockaddr{
	unsigned short  sa_family;    
	char	        sa_data[14];
};

This is a generic socket address structure which will be passed in most of the socket function calls. Here is the description of the member fields:

AttributeValuesDescription
sa_familyAF_INET
AF_UNIX
AF_NS
AF_IMPLINK
This represents an address family. In most of the Internet based applications we use AF_INET.
sa_dataProtocol Specific AddressThe 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.

Second structure that helps you to reference to the socket's elements is as follows:

struct sockaddr_in {
	short int	     sin_family;  
	unsigned short int   sin_port;	
	struct in_addr	     sin_addr;	
	unsigned char	     sin_zero[8];
};

Here is the description of the member fields:

AttributeValuesDescription
sa_familyAF_INET
AF_UNIX
AF_NS
AF_IMPLINK
This represents an address family. In most of the Internet based applications we use AF_INET.
sin_portService PortA 16 bit port number in Network Byte Order.
sin_addrIP AddressA 32 bit IP address in Network Byte Order.
sin_zeroNot UsedYou just set this value to NULL as this is not being used.

The next structure is used only in the above structure as a structure field and holds 32 but netid/hostid.

struct in_addr {
	unsigned long s_addr;
};

Here is the description of the member fields:

AttributeValuesDescription
s_addrservice portA 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; 
  char  **h_aliases; 
  int   h_addrtype;  
  int   h_length;    
  char  **h_addr_list
#define h_addr  h_addr_list[0]
};

Here is the description of the member fields:

AttributeValuesDescription
h_nameti.com etcThis is official name of the host. For example tutorialspoint.com, google.com etc.
h_aliasesTIThis will hold a list of host name aliases.
h_addrtypeAF_INETThis contains the address family and in case of Internet based application it will always be AF_INET
h_length4This will hold the length of IP address which is 4 for Internet Address.
h_addr_listin_addrFor the Internet addresses the array of pointers h_addr_list[0], h_addr_list[1] and so on are points to structure in_addr.

NOTE: h_addr is defined as h_addr_list[0] to keep backward compatibility.

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;
};

Here is the description of the member fields:

AttributeValuesDescription
s_namehttpThis is official name of the service. For example SMTP, FTP POP3 etc.
s_aliasesALIASThis will hold list of service aliases. Most of the time this will be set to NULL.
s_port80This will have associated port number. For example for HTTP this will be 80.
s_protoTCP
UDP
This will be set to the protocol used. Internet services are provided using either TCP or UDP.

Tips on Socket Structures:

Socket address structures are an integral part of every network program. We allocate them, fill them in, and pass pointers to them to various socket functions. Sometimes we pass a pointer to one of these structures to a socket function and it fills in the contents.

We always pass these structures by reference (ie. we pass a pointer to the structure, not the structure itself ), and we always pass the size of the structure as another argument.

When a socket function fills in a structure, the length is also passed by reference, so that its value can be updated by the function. We call these value-result arguments.

Always set the structure variables to NULL (i.e. '\0' ) by using memset() of bzero() functions otherwise it may get unexpected junk values in your structure.



previous next Printer Friendly

Advertisements


  

Advertisements