Pages

Socket Programming in C

Thursday 3 January 2013

                            Socket Programming in c


How To Create the TCP/IP Client Server Model?
The classic client-server model, the client sends a request to the server, and the server to do some processing, will reply to the request is received (or back) returned to the client. The terms request and the answer may have a different meaning depends on the content and the method of operation.
In addition, the server may be broad to be classified according to the method of two types of them from the customer service request. Duplicate server can service only one customer places. If two or more clients in their requests at the same time messenger, one must wait until other customers received the service. On the other hand the concurrent server can service multiple clients simultaneously. Typically, this request received from a new server process a large number of fertility original procedure returns a listen to the new connection just the process of a large number of reproductive services received request.
Transmission Control Protocol (TCP) to the end of the placement of two end points between groups. Negotiations be carried out to establish a socket, and socket still open during the entire communications. Recipient admits each packet, wrapped resend the implementation of the agreement if the packet is missed or arrives bad. TCP allows one application as it sends the data as much desire and does not depend on IP packet size limit. TCP is responsible for the data is divided into packets, buffer isolated data, re-sending lost or broken wrapped, acknowledge the receipt, by telling the sender acceleration control of rates of data streams, or slow down, for application does not receive more than it can be easily manipulated.
The target host and port number is not sufficient to identify the recipient of a TCP connection. TCP connection becomes unique five unique elements:

-          IP address of the server
-          IP address of the client
-          Port number of the server
-          Port number of the client
-          Protocol (UDP, TCP/IP, etc…)

Where each of the requesting client is assigned a unique port number in view of the server port number is always the same. If any of these numbers are different, the outlet is different. A server so you can listen to a port, conversations with multiple clients at the same time. This TCP connection is more similar to a telephone connection than a letter; You need to not only know the telephone number (IP address), but because the phone may be a lot of people to share in that positioning, you want at the other end (the port number you need) with the name of the user of its conversations. Analogy a little further sent. If you do not hear other people say, simple requests ("What?") Will lead to the other end resend or repeated phrases. Well, the connection remains open until someone hangs up.

TCP CLIENT C PROGRAM:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{
      int sock, bytes_recieved;
      char send_data[1024],recv_data[1024];
      struct hostent *host;
      struct sockaddr_in server_addr;
      host = gethostbyname("127.0.0.1");
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
                        perror("Socket");
                        exit(1);
      }
      server_addr.sin_family = AF_INET;  
      server_addr.sin_port = htons(5000);
      server_addr.sin_addr = *((struct in_addr *)host->h_addr);
      bzero(&(server_addr.sin_zero),8);
      if (connect(sock, (struct sockaddr *)&server_addr,
      sizeof(struct sockaddr)) == -1)
      {
           perror("Connect");
            exit(1);
      }
      while(1)
      {

           bytes_recieved=recv(sock,recv_data,1024,0);
           recv_data[bytes_recieved] = '\0';
          if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
          {
           close(sock);
           break;
          }
          else
           printf("\nRecieved data = %s " , recv_data);
           printf("\nSEND (q or Q to quit) : ");
           gets(send_data);
 
          if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
           send(sock,send_data,strlen(send_data), 0);
          else
          {
           send(sock,send_data,strlen(send_data), 0);
           close(sock);
           break;
          }
        }
      return 0;

}


TCP SERVER C PROGRAM:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>                                                                                                

int main()
{
        int sock, connected, bytes_recieved , true = 1;
        char send_data [1024] , recv_data[1024];    
        struct sockaddr_in server_addr,client_addr; 
        int sin_size;
   
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
       {
            perror("Socket");
            exit(1);
       }
       if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
        {
            perror("Setsockopt");
            exit(1);
        }
    
        server_addr.sin_family = AF_INET;      
        server_addr.sin_port = htons(5000);  
        server_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(server_addr.sin_zero),8);
        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))
                                                                       == -1)
      {
            perror("Unable to bind");
            exit(1);
      }
        if (listen(sock, 5) == -1)
      {
            perror("Listen");
            exit(1);
       }
                           
        printf("\nTCPServer Waiting for client on port 5000");
        fflush(stdout);
        while(1)
        {
            sin_size = sizeof(struct sockaddr_in);
            connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
            printf("\n I got a connection from (%s , %d)",
                   inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
            while (1)
            {
              printf("\n SEND (q or Q to quit) : ");
              gets(send_data);
           
              if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
              {
                send(connected, send_data,strlen(send_data), 0);
                close(connected);
                break;
              }
           
             else
                 send(connected, send_data,strlen(send_data), 0);
              bytes_recieved = recv(connected,recv_data,1024,0);
              recv_data[bytes_recieved] = '\0';
              if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
              {
                close(connected);
                break;
              }
              else
              printf("\n RECIEVED DATA = %s " , recv_data);
              fflush(stdout);
            }
        }    
      close(sock);
      return 0;
}

No comments:

Post a Comment