Friday 15 August 2014

SOCKET PROGRAMMING

Generally a socket is an endpoint communication between two systems on a network.
A socket address is a combination of IP address and Port number.
Sockets are bi-directional.
The application that initiates the communication is called a Client and the other one is called Server.

TYPES OF SOCKETS:

1. Socket Stream- connection oriented,(i.e)the first the two parties establish a connection after which any data is passed through that connection.
2. Datagram Socket- connection less(i.e) either party sends datagrams as neede and waits for the other to respond.

PROTOCOL: 

1. Initialize Winsock in the server
2. create a socket for the server
3. bind the socket-Connection
4. listen on the socket for the client
5. Now,Initialize the winsock for the Client
6. Create a socket for the Client
7. Connect to the server 
8. Server must accept the connection from the client
9. send and receive data 
10. Disconnect the chat

SERVER SIDE

          1)Initialize Winsock in the server-Create a WSADATA object called wsaData.
                        
                WSADATA wsaData; -->The WSADATA structure contains information about the Windows Sockets implementation.
    
          2) Call WSAStartup and return its value as an integer and check for errors.The WSAStartup function is called to initiate use of WS2_32.dll.
             The WSADATA structure contains information about the Windows Sockets implementation. 
             The MAKEWORD(2,2) parameter of WSAStartup makes a request for version 2.2 of Winsock on the system,
             and sets the passed version as the highest version of Windows Sockets support that the caller can use.

              Result = WSAStartup(Version, &wsaData);             
              if (Result != ZERO)
     {
               printf("WSAStartup failed with error\n");
  free(sendbuf);
                   return 1;
              }  
       else 
     {
  printf("WSAStartup success\n");
     }

                     
3) After initialization, a SOCKET object must be instantiated for use by the client.
           Declare an addrinfo object that contains a sockaddr structure and initialize these values.
           For this application, the Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned. 
           The application requests the socket type to be a stream socket for the TCP protocol.
                               

    struct addrinfo *result;
              struct addrinfo hints;

              memset(&hints, 0, sizeof(hints)); // making hints to zero
              hints.ai_family = AF_INET; // address family formats for IPv4
              hints.ai_socktype = SOCK_STREAM; // sock-stream,(reliable two way communication)
              hints.ai_protocol = IPPROTO_TCP;        // Transmission Control Protocol (TCP)
     hints.ai_flags = AI_PASSIVE; // the caller intends to use the returned socket address structure in a call to the bind function
             
         4) Call the getaddrinfo function requesting the IP address for the server name passed on the command line. 
            The TCP port on the server that the client will connect to is defined by DEFAULT_PORT as 27015 in this sample.
            The getaddrinfo function returns its value as an integer that is checked for errors. 


               Result = getaddrinfo(HOST_ADDRESS, DEFAULT_PORT, &hints, &result); // hold the address info
               if (Result != ZERO)
      {
                  printf("getaddrinfo failed\n");
 free(sendbuf);
                  return 1;
               }
     else
      {
printf("getaddrinfo success\n");
      }


5)  Create a SOCKET object called ConnectSocket.Call the socket function and return its value to the ConnectSocket variable. For this application,
            use the first IP address returned by the call to getaddrinfo that matched the address family, socket type, and protocol specified in the hints parameter.
            In this example, a TCP stream socket was specified with a socket type of SOCK_STREAM and a protocol of IPPROTO_TCP. 
            The address family was left unspecified (AF_UNSPEC), so the returned IP address could be either an IPv6 or IPv4 address for the server.
            If the client application wants to connect using only IPv6 or IPv4, then the address family needs to be set to AF_INET6 for IPv6 or AF_INET for IPv4 in the hints parameter.


ListenSocket = socket(result->ai_family,result->ai_socktype,result->ai_protocol); // Create a SOCKET to listen
                if (ListenSocket == INVALID_SOCKET)
       {
                   printf("socket not Created\n");
                   free(sendbuf);
                   return 1;
                }
       else
       {
  printf("Socket Created\n");
       }

        6)  Bind the socket

                Result = bind( ListenSocket, result->ai_addr, result->ai_addrlen); // Setup the TCP listening socket
                if (Result != ZERO)
       {
                    printf("bind failed\n");
                    free(sendbuf);
                    closesocket(ListenSocket);
                    return 1;
                }
       else
       {
    printf("bind success\n");
       }         
   

       7) Call the listen function, passing as parameters the created socket and a value for the backlog,
          maximum length of the queue of pending connections to accept.
          In this example, the backlog parameter was set to SOMAXCONN. 
          This value is a special constant that instructs the Winsock provider for this socket to allow a maximum reasonable number of pending connections in the queue.
          Check the return value for general errors.


Result = listen(ListenSocket,0);            // listening on the socket for the client
                if (Result != ZERO) 
       {
                   printf("listen failed\n");
  free(sendbuf);
                   closesocket(ListenSocket);
                   return 1;
                }
       else 
       {
  printf("Listen success\n");
       }

      8) Once the socket is listening for a connection, the program must handle connection requests on that socket.
         Create a temporary SOCKET object called ClientSocket for accepting connections from clients.


              ClientSocket = accept(ListenSocket, NULL, NULL);    // Accept a client socket
              if (ClientSocket == INVALID_SOCKET)
     {
                printf("accept failed\n");
free(sendbuf);
                closesocket(ListenSocket);
                return 1;
              }
     else
     {
printf("Accept success\n");
     }

      

CLIENT SIDE

    1) Similarly initialize winsock,like you did in server.After initialization, a SOCKET object must be instantiated for use by the client.
       create a socket.Declare an addrinfo object that contains a sockaddr structure and initialize these values.
       For this application, the Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned. 
       The application requests the socket type to be a stream socket for the TCP protocol.


        WSADATA wsaData;                                  
                 Result = WSAStartup(Version, &wsaData); // Initialize Winsock
                 if (Result != 0)
        {
                    printf("WSAStartup failed\n");
   free(sendbuf);
                    return 1;
                 }
        else
        {
   printf("WSAStartup success\n");
        }

                struct addrinfo *result;                           
                struct addrinfo hints;                              

                memset(&hints, 0, sizeof(hints));        // making hints to zero
                hints.ai_family = AF_INET;       // address family formats for IPv4
                hints.ai_socktype = SOCK_STREAM;     // sock-stream,(reliable two way communication)
                hints.ai_protocol = IPPROTO_TCP;      // Transmission Control Protocol (TCP)


2) Call the getaddrinfo function requesting the IP address for the server name passed on the command line.
           The TCP port on the server that the client will connect to is defined by DEFAULT_PORT as 27015 in this sample. 
           The getaddrinfo function returns its value as an integer that is checked for errors.

                 Result = getaddrinfo(LOOP_ADDRESS,DEFAULT_PORT, &hints, &result);         // hold address information
                 if (Result != 0)  
        {
                     printf("getaddrinfo failed\n");
    free(sendbuf);                            
                     return 1;
                 }
        else
       {
    printf("getaddrinfo success\n");
       }

3) Call the socket function and return its value to the ConnectSocket variable.
           For this application, use the first IP address returned by the call to getaddrinfo that matched the address family, socket type, and protocol specified in the hints parameter.
           In this example, a TCP stream socket was specified with a socket type of SOCK_STREAM and a protocol of IPPROTO_TCP.The address family was left unspecified (AF_UNSPEC), 
           so the returned IP address could be either an IPv6 or IPv4 address for the server.If the client application wants to connect using only IPv6 or IPv4, 
           then the address family needs to be set to AF_INET6 for IPv6 or AF_INET for IPv4 in the hints parameter.


ConnectSocket=socket(result->ai_family,result->ai_socktype,result->ai_protocol); // Creating a SOCKET with necessary parameters
                if (ConnectSocket == INVALID_SOCKET)
       {
                   printf("socket creation failed\n");
  free(sendbuf);
                   return 1;
                }
       else
       {
  printf("socket created\n");
       }

4) Call the connect function, passing the created socket and the sockaddr structure as parameters. Check for general errors.

Result = connect(ConnectSocket, result->ai_addr,result->ai_addrlen);    // Connect to server
                         if(Result != ZERO)
                {
                           printf("Unable to connect to server!\n");
         _getch();
          closesocket(ConnectSocket);                                        // closing the created socket
          free(sendbuf);
          return 1;
                }
                           printf("server connected\n");



THAT'S ALL NOW HERE IS THE SNIPPET FOR SENDING AND RECEIVING-COMMON FOR BOTH

        printf("\n*** For Disconnecting the Chat don't type anything,just simply press 'ENTER' ***\n"); 
while(1)
{
printf("\nCLIENT: ");
fflush(stdin);     // clear the stdin buffer
gets(sendbuf); 
int string_len=strlen(sendbuf);
Send_Result = send(ConnectSocket,sendbuf,string_len,0);        
if(Send_Result==0)
{
break;
}
Recv_Result = recv(ConnectSocket,recvbuf,MAX_LEN,0);
if(Recv_Result==0)
{
break;
}
recvbuf[Recv_Result]='\0';                                          // appending a null character
printf("\nSERVER: %s\n",recvbuf);
}
free(sendbuf);
closesocket(ConnectSocket);   
return 0;
}
NOTE:the IP is LOOP BACK 127.0.0.1 if you r running it on the same computer


OUTPUTS:

---->>1st run the server





----->client is trying to connect





NOW the client is connected you can chat



Author
Aravind A
Project Engineer

   

No comments:

Post a Comment