Search This Blog

Saturday, 5 July 2014

EX1: Write a program to transfer a simple message from client to server using TCP socket



EX1: Write a program to transfer a simple message from client to server using TCP socket

Algorithm:

Server:

1.      Create a socket with the socket() system call.
2.      Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3.      Listen for connections with the listen() system call.
4.      Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
5.      Send and receive data using the read() and write() system calls.
6.      Terminate the connections using close() system call.

Client:

1.      Create a socket with the socket() system call.
2.      Connect the socket to the address of the server using the connect() system call.
3.      Send and receive data using the read() and write() system calls.


Server Program:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
#include<time.h>
#define MAX 100
#define len 81

int cwork(int);
int main()
{
  int smain,sclient,child,port=5678;
  struct sockaddr_in serv;
  char str[100];
  if((smain=socket(AF_INET,SOCK_STREAM,0))<0)
  {
    printf("\nserver cannot open main socket");
    exit(0);
  }
  bzero(&serv,sizeof(serv));
  serv.sin_family=AF_INET;
  serv.sin_addr.s_addr=htonl(INADDR_ANY);
  serv.sin_port=htons(port);
  if(bind(smain,(struct sockaddr *)&serv, sizeof(serv))<0)
  {
    printf("\nServer bin fail");
    exit(0);
  }
  listen(smain,15);
  for(;;)
  {
    if((sclient=accept(smain,0,0))<0)
    {
      printf("\nClient is bad");
      exit(0);
    }
    if((child=fork())<0)
    {
      printf("\nFailed to creat child");
      exit(1);
    }
    else if(child==0)
    {
      close(smain);
      cwork(sclient);
      close(sclient);
      exit(0);
    }
    close(sclient);
  }
}

int cwork(int sclient)
{
  char buf[len];
  int msglen;
  bzero(buf,len);
  if((msglen=recv(sclient,buf,len,0))<0)
  {
    printf("\nerror during child reception");
    exit(1);
  }
  printf("\nScoket is used %d",sclient);
  buf[msglen]='\0';
  printf("\nMessage %s",buf);
  strcpy(buf,"message recd bye");
  send(sclient,buf,strlen(buf),0);
}

Client Program:

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
#include<time.h>
#define MAX 100
#define len 81

int cwork(int);
int main(int argc,char **argv)
{
        int sockfd,n;
        struct sockaddr_in serv;
        char str[100];
        if(argc!=3)
        {
                printf("\n error message");
        }
        if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
        {
                printf("\n socket not created");
                exit(0);
        }
        bzero(&serv,sizeof(serv));
        serv.sin_family=AF_INET;
        serv.sin_port=htons(atoi(argv[2]));
        if(inet_pton(AF_INET,argv[1],&serv.sin_addr) <=0 )
        {
                printf("\n conversion error");
                exit(0);
        }
        if(connect(sockfd, (struct sockaddr *)&serv, sizeof(serv)) <0)
        {
                printf("\n connect fail");
                exit(0);
        }
        printf("\n connected");
        printf("\n enter the message to server");
        scanf("%s",str);
        write(sockfd,str,sizeof(str));
        n=read(sockfd,str,100);
        str[n]='\0';
        printf("\n from server %s", str);

}


OUTPUT:

SERVER:

[3019@tecnetserver anand]$ cc tcpserver.c -o server
[3019@tecnetserver anand]$ ./server 127.0.0.1 3019

Socket is used 4
 Message Hello

CLIENT:

[3019@tecnetserver anand]$ cc tcpclient.c -o client
[3019@tecnetserver anand]$ ./client 127.0.0.1 3019

Connected
Enter the message to server Hello

From server Message read bye

Socket Core Functions



This tutorial describes the core socket functions required to write a complete TCP client and server.
Following is the diagram showing complete Client and Server interaction:





The socket Function:

To perform network I/O, the first thing a process must do is call the socket function, specifying the type of communication protocol desired and protocol family etc.
#include <sys/types.h>
#include <sys/socket.h>
 
int socket (int family, int type, int protocol);
This call gives you a socket descriptor that you can use in later system calls or it gives you -1 on error.

Parameters:

family: specifies the protocol family and is one of the constants shown below:

Family
Description
AF_INET
IPv4 protocols
AF_INET6
IPv6 protocols
AF_LOCAL
Unix domain protocols
AF_ROUTE
Routing Sockets
AF_KEY
Ket socket

This tutorial does not talk about other protocols except IPv4.
type: specifies kind of socket you want. It can take one of the following values:

Type
Description
SOCK_STREAM
Stream socket
SOCK_DGRAM
Datagram socket
SOCK_SEQPACKET
Sequenced packet socket
SOCK_RAW
Raw socket

protocol: argument should be set to the specific protocol type given below or 0 to select the system's default for the given combination of family and type:

Protocol
Description
IPPROTO_TCP
TCP transport protocol
IPPROTO_UDP
UDP transport protocol
IPPROTO_SCTP
SCTP transport protocol

The connect Function:

The connect function is used by a TCP client to establish a connection with a TCP server.
#include <sys/types.h>
#include <sys/socket.h>
 
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
This call returns 0 if it successfully connects to the server otherwise it gives you -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         serv_addr is a pointer to struct sockaddr that contains destination IP address and port.
·         addrlen set it to sizeof(struct sockaddr).

The bind Function:

The bind function assigns a local protocol address to a socket. With the Internet protocols, the protocol address is the combination of either a 32-bit IPv4 address or a 128-bit IPv6 address, along with a 16-bit TCP or UDP port number. This function is called by TCP server only.
#include <sys/types.h>
#include <sys/socket.h>
 
int bind(int sockfd, struct sockaddr *my_addr,int addrlen);
This call returns 0 if it successfully binds to the address otherwise it gives you -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         my_addr is a pointer to struct sockaddr that contains local IP address and port.
·         addrlen set it to sizeof(struct sockaddr).

You can put your IP address and your port automatically:
A 0 value for port number means system will choose a random port and INADDR_ANY value for IP address means server's IP address will be assigned automatically.
server.sin_port = 0;                
server.sin_addr.s_addr = INADDR_ANY;

NOTE: As descript in Ports and Services tutorials, all ports bellow 1024 are reserved. So you can set a port above 1024 and bellow 65535 unless the ones being used by other programs.

The listen Function:

The listen function is called only by a TCP server and it performs two actions:
·         The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket.
·         The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.
#include <sys/types.h>
#include <sys/socket.h>
 
int listen(int sockfd,int backlog);
This call returns 0 on success otherwise it gives you -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         backlog is the number of allowed connections.

The accept Function:

The accept function is called by a TCP server to return the next completed connection from the front of the completed connection queue. Following is the signature of the call:
#include <sys/types.h>
#include <sys/socket.h>
 
int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
This call returns non negative descriptor on success otherwise it gives you -1 on error. The returned decriptor is assumed to be a client socket descriptor and all read write operations will be done on this descripton to communicate with the client.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         cliaddr is a pointer to struct sockaddr that contains client IP address and port.
·         addrlen set it to sizeof(struct sockaddr).

The send Function:

The send function is used to send data over stream sockets or CONNECTED datagram sockets. If you want to send data over UNCONNECTED datagram sockets you must use sendto() function.
You can use write() system call to send the data. This call is explained in helper functions tutorial.
int send(int sockfd, const void *msg, int len, int flags);
This call returns the number of bytes sent out otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         msg is a pointer to the data you want to send.
·         len is the length of the data you want to send (in bytes).
·         flags is set to 0.

The recv Function:

The recv function is used to receive data over stream sockets or CONNECTED datagram sockets. If you want to receive data over UNCONNECTED datagram sockets you must use recvfrom().
You can use read() system call to read the data. This call is explained in helper functions tutorial.
int recv(int sockfd, void *buf, int len, unsigned int flags);
This call returns the number of bytes read into the buffer otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         buf is the buffer to read the information into.
·         len is the maximum length of the buffer.
·         flags is set to 0.

The sendto Function:

The sendto function is used to send data over UNCONNECTED datagram sockets. Put simply, when you use scoket type as SOCK_DGRAM
int sendto(int sockfd, const void *msg, int len, unsigned int flags,
                  const struct sockaddr *to, int tolen);
This call returns the number of bytes sent otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         msg is a pointer to the data you want to send.
·         len is the length of the data you want to send (in bytes).
·         flags is set to 0.
·         to is a pointer to struct sockaddr for the host where data has to be sent.
·         tolen is set it to sizeof(struct sockaddr).

The recvfrom Function:

The recvfrom function is used to receive data from UNCONNECTED datagram sockets. Put simply, when you use scoket type as SOCK_DGRAM
int recvfrom(int sockfd, void *buf, int len, unsigned int flags
                    struct sockaddr *from, int *fromlen);
This call returns the number of bytes read into the buffer otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         buf is the buffer to read the information into.
·         len is the maximum length of the buffer.
·         flags is set to 0.
·         from is a pointer to struct sockaddr for the host where data has to be read.
·         fromlen is set it to sizeof(struct sockaddr).

The close Function:

The close function is used to close the communication between client and server.
int close( int sockfd );
This call returns 0 on success otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.

The shutdown Function:

The shutdown function is used to gracefully close the communication between client and server. This function gives more control in caomparision of close function.
int shutdown(int sockfd, int how);
This call returns 0 on success otherwise it will return -1 on error.

Parameters:

·         sockfd: is a socket descriptor returned by the socket function.
·         how: put one of the numbers:
o        0 indicates receives disallowed,
o        1 indicatesthat sends disallowed and
o        2 indicates that sends and receives disallowed. When how is set to 2, it's the same thing as close().

The select Function:

The select function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending.
When an application calls recv or recvfrom it is blocked until data arrives for that socket. An application could be doing other useful processing while the incoming data stream is empty. Another situation is when an application receives data from multiple sockets.
Calling recv or recvfrom on a socket that has no data in it's input queue prevents immediate reception of data from other sockets. The select function call solves this problem by allowing the program to poll all the socket handles to see if they are available for non-blocking reading and writing operations.
 int select(int  nfds,  fd_set  *readfds,  fd_set  *writefds,
     fd_set *errorfds, struct timeval *timeout);
This call returns 0 on success otherwise it will return -1 on error.

Parameters:

·         nfds: specifies the range of file descriptors to be tested. The select() function tests file descriptors in the range of 0 to nfds-1
·         readfds:points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read. Can be NULL to indicate an empty set.
·         writefds:points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write Can be NULL to indicate an empty set.
·         exceptfds :points to an object of type fd_set that on input specifies the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending. Can be NULL to indicate an empty set.
·         timeout :poins to a timeval struct that specifies how long the select call should poll the descriptors for an available I/O operation. If the timeout value is 0, then select will return immediately. If the timeout argument is NULL, then select will block until at least one file/socket handle is ready for an available I/O operation. Otherwise select will return after the amount of time in the timeout has elapsed OR when at least one file/socket descriptor is ready for an I/O operation.

The return value from select is the number of handles specified in the file descriptor sets that are ready for I/O. If the time limit specified by the timeout field is reached, select return 0. The following macros exist for manipulating a file descriptor set:
·         FD_CLR(fd, &fdset): Clears the bit for the file descriptor fd in the file descriptor set fdset
·         FD_ISSET(fd, &fdset): Returns a non-zero value if the bit for the file descriptor fd is set in the file descriptor set pointed to by fdset, and 0 otherwise.
·         FD_SET(fd, &fdset): Sets the bit for the file descriptor fd in the file descriptor set fdset.
·         FD_ZERO(&fdset): Initializes the file descriptor set fdset to have zero bits for all file descriptors.
The behavior of these macros is undefined if the fd argument is less than 0 or greater than or equal to FD_SETSIZE.

The write Function:

The write function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.
You can also use send() function to send data to another process.
#include <unistd.h>
 
int write(int fildes, const void *buf, int nbyte);
Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned,

Parameters:

·         fildes: is a socket descriptor returned by the socket function.
·         buf is a pointer to the data you want to send.
·         nbyte is the number of bytes to be written. If nbyte is 0, write() will return 0 and have no other results if the file is a regular file; otherwise, the results are unspecified.

The read Function:

The read function attempts to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.
You can also use recv() function to read data to another process.
#include <unistd.h>
 
int read(int fildes, const void *buf, int nbyte);
Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned,

Parameters:

·         fildes: is a socket descriptor returned by the socket function.
·         buf is the buffer to read the information into..
·         nbyte is the number of bytes to read.

The fork Function:

The fork function create a new process. The new process is called child process will be an exact copy of the calling process (parent process). The child process inherits many attributes from the parent process.
#include <sys/types.h>
#include <unistd.h>
 
int fork(void);
Upon successful completion, fork() return 0 to the child process and return the process ID of the child process to the parent process. Otherwise -1 is returned to the parent process, no child process is created and errno is set to indicate the error.

Parameters:

·         void: means no parameter is required.

The bzero Function:

The bzero function places nbyte null bytes in the string s. This function will be used to set all the socket structures with null values.
 void bzero(void *s, int nbyte);
This function does not return anything.

Parameters:

·         s: specifies string which has to be filled with null bytes.This will be a point to socket structure variable
·         nbyte: specifies the number of bytes to be filled with null values. This will be the size of the socket structure.

The bcmp Function:

The bcmp function compares byte string s1 against byte string s2. Both strings are assumed to be nbyte bytes long.
int bcmp(const void *s1, const void *s2, int nbyte);
This function returns 0 if both strings are identical, 1 otherwise. The bcmp() function always returns 0 when nbyte is 0.

Parameters:

·         s1: specifies the first string to be compared.
·         s2: specifies the second string to be compared.
·         nbyte: specifies the number of bytes to be compared.

The bcopy Function:

The bcopy function copies nbyte bytes from string s1 to the string s2. Overlapping strings are handled correctly.
void bcopy(const void *s1, void *s2, int nbyte);
This function does not return anything.

Parameters:

·         s1: specifies the source string.
·         s2: specifies the destination string.
·         nbyte: specifies the number of bytes to be copied.

The memset Function:

The memset function is also used to set structure variables in the same way as bzero.
void *memset(void *s, int c, int nbyte);
This function returns a pointer to void, in fact pointer to the set memory and you need to caste it accordingly.

Parameters:

·         s: specifies the source to be set.
·         c: specifies the character to set on nbyte places..
·         nbyte: specifies the number of bytes to be set.

 
Video tutorials: 

http://youtu.be/DSmQHfqZkws 

http://youtu.be/G-oaJaZfy9A 

http://youtu.be/0ajmWvg0UUs 



 

Wednesday, 2 July 2014

Socket Programing



Socket:

Sockets allow communication between two different processes on the same or different machines.

Socket Types:

There are four types of sockets available to the users. The first two are most commenly used and last two are rarely used.
Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types.

·       Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A,B,C", they will arrive in the same order - "A,B,C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do no have any boundaries.
·  Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).
·     Raw Sockets: provides users access to the underlying communication protocols which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more esoteric facilities of an existing protocol.
·      Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.

 

Network Addresses - IP Addreses

Before we proceed with actual stuff, lets understand a but about the Network Addresses - The IP Address.
The IP host address, or more commonly just IP address, is used to identify hosts connected to the Internet. IP stands for Internet Protocol and refers to the Internet Layer of the overall network architecture of the Internet.
An IP address is a 32-bit quantity interpreted as 4 8-bit numbers or octets. Each IP address uniquely identifies the participating user network, the host on the network, and the class of the user network.
An IP address is usually written in a dotted-decimal notation of the form N1.N2.N3.N4, where each Ni is a decimal number between 0 and 255 decimal (00 through FF hexadecimal).

 

Address Classes:

IP addresses are managed and created by the Internet Assigned Numbers Authority (IANA). There are 5 different address classes. You can determine which class any IP address is in by examining the first 4 bits of the IP address.
  • Class A addresses begin with 0xxx, or 1 to 126 decimal.
  • Class B addresses begin with 10xx, or 128 to 191 decimal.
  • Class C addresses begin with 110x, or 192 to 223 decimal.
  • Class D addresses begin with 1110, or 224 to 239 decimal.
  • Class E addresses begin with 1111, or 240 to 254 decimal.

Addresses beginning with 01111111, or 127 decimal, are reserved for loopback and for internal testing on a local machine; [ You can test this: you should always be able to ping 127.0.0.1, which points to yourself ] Class D addresses are reserved for multicasting; Class E addresses are reserved for future use. They should not be used for host addresses.

 

Example:

Class
Leftmost bits
Start address
Finish address
A
0xxx
0.0.0.0
127.255.255.255
B
10xx
128.0.0.0
191.255.255.255
C
110x
192.0.0.0
223.255.255.255
D
1110
224.0.0.0
239.255.255.255
E
1111
240.0.0.0
255.255.255.255
 

 

Subnetting:

Subnetting an IP Network can be done for a variety of reasons, including organization, use of different physical media (such as Ethernet, FDDI, WAN, etc.), preservation of address space, and security. The most common reason is to control network traffic.
The basic idea in subnetworking (a common word also used is subnetting) is to partition the host identifier portion of the IP address into two parts:
  1. A subnet address within the network address itself; and
  2. A host address on the subnet.
For example, a common Class B address format is N1.N2.S.H, where N1.N2 identifies the Class B network, the 8-bit S field identifies the subnet, and the 8-bit H field identifies the host on the subnet.
This is very difficult to remember many Host Names in terms of numbers. So these host names are generally known by "ordinary" names such as takshila or nalanda. We write software application's to find out the dotted IP address corresponding to a given name.
The process of finding out dotted IP address based on the given alphanumeric host name is known as hostname resolution.
A hostname resolution is done by special softwares residing on a high capacity systems. These system are called Domain Name Systems (DNS) which keeps mapping of IP addresses and corresponding ordinary names.

 

How to make client:

The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. The two processes each establish their own sockets.
The steps involved in establishing a socket on the client side are as follows:
1.      Create a socket with the socket() system call.
2.      Connect the socket to the address of the server using the connect() system call.
3.      Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

 

How to make a server:

The steps involved in establishing a socket on the server side are as follows:
1.      Create a socket with the socket() system call.
2.      Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3.      Listen for connections with the listen() system call.
4.      Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
5.      Send and receive data using the read() and write() system calls.

 

Client and Server Interaction:

Following is the diagram showing complete Client and Server interaction:




Socket Structures

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:
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.
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:
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.


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:
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; 
  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:
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.

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:
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.

 

Ports and Services


When a client process wants to connect a server, the client must have a way of identifying the server that it wants to connect. SO if the client knows the 32-bit Internet address of the host on which the server resides it can contact that host. But how does the client identify the particular server process running on that host ?
To resolve the problem of identifying a particular server process running on a host, both TCP and UDP have defined a group of well known ports.
For our purposes, a port will be defined as an integer number between 1024 and 65535. This is because all port numbers smaller than 1024 are considered well-known -- for example, telnet uses port 23, http uses 80, ftp uses 21, and so on.
The port assignments to network services can be found in the file /etc/services. If you are writing your own server then care must be taken to assign a port to your server. You should make sure that this port should already be not assigned to any other server.
Normally its a practice to assign any port number more than 5000. But there are many organizations who has written servers having port number more than 5000. For example Yahoo Messanger runs on 5050, SIP Server runs on 5060 etc.

Example Ports and Services:

Here is a small list of services and associated ports. You can find most updated list of internet ports and associated service at IANA - TCP/IP Port Assignments.
Service
Port Number
Service Description
echo
7
UDP/TCP sends back what it receives
discard
9
UDP/TCP throws away input
daytime
13
UDP/TCP returns ASCII time
chargen
19
UDP/TCP returns characters
ftp
21
TCP file transfer
telnet
23
TCP remote login
smtp
25
TCP email
daytime
37
UDP/TCP returns binary time
tftp
69
UDP trivial file transfer
finger
79
TCP info on users
http
80
TCP World Wide Web
login
513
TCP remote login
who
513
UDP different info on users
Xserver
6000
TCP X windows (N.B. >1023)

 

Port and Service Functions:


Unix provides following functions to fetch service name from the /etc/services file.
·       struct servent *getservbyname(char *name, char *proto): - This call takes service name and protocol name and returns corresponding port number for that service.
·         struct servent *getservbyport(int port, char *proto): - This call takes port number and protocol name and returns corresponding service name.
The return value for each function is a pointer to a structure with the following form:
struct servent
{
  char  *s_name; 
  char  **s_aliases; 
  int   s_port;  
  char  *s_proto;
};
Here is the description of the member fields:
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.


Network Byte Orders


Unfortunately, note all computers store the bytes that comprise a multibyte value in the same order. Consider a 16 bit interget that is made up of 2 bytes. There are two ways to store this value.
·         Little Endian: In this scheme low-order byte is stored on the starting address (A) and high-order byte is stored on the next address (A + 1).
·         Big Endian: In this scheme high-order byte is stored on the started address (A) and low-order byte is stored on the next address (A+1).
So that machines with different byte order conventions can communicate, the Internet protocols specify a canonical byte order convention for data transmitted over the network. This is known as Network Byte Order.
When establishing an Internet socket connection, you must make sure that the data in the sin_port and sin_addr members of the sockaddr_in structure are represented in Network Byte Order.

 

Byte Ordering Functions:


Routines for converting data between a host's internal representation and Network Byte Order are:
Function
Description
htons()
Host to Network Short
htonl()
Host to Network Long
ntohl()
Network to Host Long
ntohs()
Network to Host Short

Here is more detail of these functions:
·         unsigned short htons(unsigned short hostshort)
This function converts 16-bit (2-byte) quantities from host byte order to network byte order.
·         unsigned long htonl(unsigned long hostlong)
This function converts 32-bit (4-byte) quantities from host byte order to network byte order.
·         unsigned short ntohs(unsigned short netshort)
This function converts 16-bit (2-byte) quantities from network byte order to host byte order.
·         unsigned long ntohl(unsigned long netlong)
This function converts 32-bit quantities from network byte order to host byte order.
These functions are macros and result in the insertion of conversion source code into the calling program. On little-endian machines the code will change the values around to network byte order. On big-endian machines no code is inserted since none is needed; the functions are defined as null.

 

IP Address Functions


Unix provides various function calls that will help you manipulating IP addresses. These functions convert Internet addresses between ASCII strings (what humans prefer to use) and network byte ordered binary values (values that are stored in socket address structures).
There are following three function calls which are used for IPv4 addressing:
(1) int inet_aton(const char *strptr, struct in_addr *addrptr):
This function call converts the specified string, in the Internet standard dot notation, to a network address, and stores the address in the structure provided. The converted address will be in Network Byte Order (bytes ordered from left to right). This returns 1 if string was valid and 0 on error.
Following is the usage example:
#include <arpa/inet.h>
 
(...)
    int retval;
    struct in_addr addrptr
    
    memset(&addrptr, '\0', sizeof(addrptr));
    retval = inet_aton("68.178.157.132", &addrptr); 
 
(...)
(2) in_addr_t inet_addr(const char *strptr):
This function call converts the specified string, in the Internet standard dot notation, to an integer value suitable for use as an Internet address. The converted address will be in Network Byte Order (bytes ordered from left to right). This returns a 32-bit binary network byte ordered IPv4 address and INADDR_NONE on error.
Following is the usage example:
#include <arpa/inet.h>
 
(...)
 
    struct sockaddr_in dest;
 
    memset(&dest, '\0', sizeof(dest));
    dest.sin_addr.s_addr = inet_addr("68.178.157.132"); 
 
(...)
(3) char *inet_ntoa(struct in_addr inaddr):
This function call converts the specified Internet host address to a string in the Internet standard dot notation.
Following is the usage example:
#include <arpa/inet.h>
 
(...)
 
        char *ip;
 
        ip=inet_ntoa(dest.sin_addr);
 
        printf("IP Address is: %s\n",ip);
 
(...)