Friday 20 May 2011

Steps involved in creating a client

Following are the major steps involved in creating a 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. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

Steps involved in creating a server

Following are the major steps involved in creating a  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

Wednesday 18 May 2011

Code for finding Ip address of server

#include <stdio.h> /*using for printf*/
#include <netdb.h> /*used for gethostbyname() function*/
#include <sys/types.h>

int main(int argc, char *argv[])
{
  struct hostent *he;/*making a structure to hold all the info related to host like hostname and host ip*/
  if (argc!=2)/*if no arguments are passed*/
  {
  argv[1]="www.google.com";/*then make www.google.com as our default option*/
  }
  he=gethostbyname(argv[1]);/*fills the he structure instance with hostname and ip address*/
  if (he==NULL)/*if he is NULL that means the host is not found*/
  {
  printf("gethostbyname() error\n");
  exit(-1);/*exit the program with error*/
  }
  printf("Hostname : %s\n",he->h_name); /* prints the hostname */
  /*inet_ntoa converts string IP addresses to long*/
  printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr))); /* prints IP address */
}




Sunday 15 May 2011

IMPORTANT FUNCTIONS

I will mention some of the important functions used in socket programming and which header files to include in your program:

socket()
 #include <sys/types.h>
 #include <sys/socket.h>

 int socket(int domain,int type,int protocol);


 Let's see the arguments:

  domain -> you can set "AF_INET" (set AF_INET to use ARPA internet protocols)
  or "AF_UNIX" if you want to create sockets for inside comunication.
  Those two are the most used, but don't think that there are just
  those. There are more I just don't mention them.
  type -> here you put the kind of socket you want (Stream or Datagram)
  If you want Stream Socket the type must be SOCK_STREAM
  If you want Datagram Socket the type must be SOCK_DGRAM
  protocol -> you can just set protocol to 0


 socket() gives you a socket descriptor that you can use in later system calls or
 it gives you -1 on error (this is usefull for error checking routines).

bind()


 #include <sys/types.h>
 #include <sys/socket.h>

 int bind(int fd, struct sockaddr *my_addr,int addrlen);


 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  my_addr -> is a pointer to struct sockaddr
  addrlen -> set it to sizeof(struct sockaddr)

 bind() is used when you care about your local port (usually when you use listen() )
 and its function is to associate a socket with a port (on your machine). It returns
 -1 on error.

 You can put your IP address and your port automatically:

  server.sin_port = 0; /* bind() will choose a random port*/
  server.sin_addr.s_addr = INADDR_ANY; /* puts server's IP automatically */


 An important aspect about ports and bind() is that all ports bellow 1024 are reserved.
 You can set a port above 1024 and bellow 65535 (unless the ones being used by other
 programs).

connect()

 #include <sys/types.h>
 #include <sys/socket.h>

 int connect(int fd, struct sockaddr *serv_addr, int addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  serv_addr -> is a pointer to struct sockaddr that contains destination IP
  address and port
  addrlen -> set it to sizeof(struct sockaddr)

 connect() is used to connect to an IP address on a defined port. It returns -1 on
 error.


 listen()
 #include <sys/types.h>
 #include <sys/socket.h>

 int listen(int fd,int backlog);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  backlog -> is the number of allowed connections

 listen() is used if you're waiting for incoming connections, this is, if you want
 someone to connect to your machine. Before calling listen(), you must call bind()
 or you'll be listening on a random port =) After calling listen() you must call
 accept() in order to accept incoming connection. Resuming, the sequence of system calls
 is:

  1. socket()
  2. bind()
  3. listen()
  4. accept() /* In the next section I'll explain how to use accept() */

 As all functions above described, listen() returns -1 on error.


accept()

 #include <sys/socket.h>

 int accept(int fd, void *addr, int *addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by listen() call
  addr -> is a pointer to struct sockaddr_in where you can determine which host
  is calling you from which port
  addrlen -> set it to sizeof(struct sockaddr_in) before its address is passed
  to accept()

 send()

  int send(int fd,const void *msg,int len,int flags);

 Let's see the arguments:

  fd -> is the socket descriptor where you want to send data to
  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 -> set it to 0


 This 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().

 send() returns the number of bytes sent out and it will return -1 on error.


recv()

 int recv(int fd, void *buf, int len, unsigned int flags);

 Let's see the arguments:

  fd -> is the socket descriptor to read from
  buf -> is the buffer to read the information into
  len -> is the maximum length of the buffer
  flags -> set it to 0


 As I said above about send(), this 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 recvfrom().

 recv() returns the number of bytes read into the buffer and it'll return -1 on error.


sendto()
 
 int sendto(int fd,const void *msg, int len, unsigned int flags,
  const struct sockaddr *to, int tolen);

 Let's see the arguments:

  fd -> the same as send()
  msg -> the same as send()
  len -> the same as send()
  flags -> the same as send()
  to -> is a pointer to struct sockaddr
  tolen -> set it to sizeof(struct sockaddr)

 As you can see, sendto() is just like send(). It has only two more arguments : "to"
 and "tolen" =)

 sendto() is used for UNCONNECTED datagram sockets and it returns the number of bytes
 sent out and it will return -1 on error.


recvfrom()

 int recvfrom(int fd,void *buf, int len, unsigned int flags
  struct sockaddr *from, int *fromlen);

 Let's see the arguments:

  fd -> the same as recv()
  buf -> the same as recv()
  len -> the same as recv()
  flags -> the same as recv()
  from -> is a pointer to struct sockaddr
  fromlen -> is a pointer to a local int that should be initialised
  to sizeof(struct sockaddr)

 recvfrom() returns the number of bytes received and it'll return -1 on error.


  close()

 close(fd);

 close() is used to close the connection on your socket descriptor. If you call close(),
 it won't be no more writes or reads and if someone tries to read/write will receive an
 error.


shutdown()

 int shutdown(int fd, int how);

 Let's see the arguments:

  fd -> is the socket file descriptor you want to shutdown
  how -> you put one of those numbers:
 
  0 -> receives disallowed
  1 -> sends disallowed
  2 -> sends and receives disallowed

 When how is set to 2, it's the same thing as close().

 shutdown() returns 0 on success and -1 on error.


 gethostname()

 #include <unistd.h>

 int gethostname(char *hostname, size_t size);

 Let's see the arguments:

  hostname -> is a pointer to an array that contains hostname
  size -> length of the hostname array (in bytes)


 gethostname() is used to get the name of the local machine.

Functions Related to IP Adress

In C, there are some functions that will help you manipulating IP addresses.

inet_addr() converts an IP address into an unsigned long.
inet_ntoa() converts string IP addresses to long.

Sockets Conversion Functions

There are two types of byte ordering: most significant byte and least significant byte.
This former is called "Network Byte Order" and some machines store their numbers internally
in Network Byte Order.

There are two types you can convert: short and long.
Imagine you want to convert a long from Host Byte Order to Network Byte Order.

 The following functions are used to convert :

 htons() -> "Host to Network Short"
 htonl() -> "Host to Network Long"
 ntohs() -> "Network to Host Short"
 ntohl() -> "Network to Host Long"

Saturday 14 May 2011

Socket Programming in C

What are Sockets?
 Sockets provide a standard connection protocol which enables the communication of data over a network.

 Sockets supports both connection oriented (Transmission Control Protocol) and connectionless (User Datagram Protocol)
 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 */ 
          };

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.

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

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.

struct in_addr
{
         unsigned long    s_addr;
};


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; /* 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. */
 };


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.

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


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.