Tuesday, 14 January 2014

Client-Server Program to remotely Calculate Operations on 2 Natural Numbers

Client


#include <iostream>

#include <cstring>      // Needed for memset

#include <sys/socket.h> // Needed for the socket functions

#include <netdb.h>      // Needed for the socket functions

int main()

{

    int status;

    struct addrinfo host_info;       // The struct that getaddrinfo() fills up with data.

    struct addrinfo *host_info_list; // Pointer to the to the linked list of host_info's.

    memset(&host_info, 0, sizeof host_info);

    std::cout << "Setting up the structs..."  << std::endl;

    host_info.ai_family = AF_UNSPEC;     // IP version not specified. Can be both.

    host_info.ai_socktype = SOCK_STREAM; // Use SOCK_STREAM for TCP or SOCK_DGRAM for UDP.

    // Now fill up the linked list of host_info structs with google's address information.

    status = getaddrinfo("127.0.0.1", "5556", &host_info, &host_info_list);

    // getaddrinfo returns 0 on succes, or some other value when an error occured.

    // (translated into human readable text by the gai_gai_strerror function).

    if (status != 0)  std::cout << "getaddrinfo error" << gai_strerror(status) ;

    std::cout << "Creating a socket..."  << std::endl;

    int socketfd ; // The socket descripter

    socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,

                      host_info_list->ai_protocol);

    if (socketfd == -1)  std::cout << "socket error " ;

    std::cout << "Connect()ing..."  << std::endl;

    status = connect(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);

    if (status == -1)  std::cout << "connect error" ;

    std::cout << "send()ing message..."  << std::endl;

    char msg[20];

    std::cout<<"Enter expression: ";

    std::cin>>msg;

    int len;

    ssize_t bytes_sent;

    len = strlen(msg);

    bytes_sent = send(socketfd, msg, len, 0);

    std::cout << "Waiting to recieve data..."  << std::endl;

    ssize_t bytes_recieved;

    char incomming_data_buffer[1000];

    bytes_recieved = recv(socketfd, incomming_data_buffer,1000, 0);

    // If no data arrives, the program will just wait here until some data arrives.

    if (bytes_recieved == 0) std::cout << "host shut down." << std::endl ;

    if (bytes_recieved == -1)std::cout << "recieve error!" << std::endl ;

    std::cout << bytes_recieved << " bytes recieved :" << std::endl ;

    incomming_data_buffer[bytes_recieved] = '\0' ;

    std::cout << incomming_data_buffer << std::endl;

    std::cout << "Receiving complete. Closing socket..." << std::endl;

    freeaddrinfo(host_info_list);

    close(socketfd);

}



Server


#include <iostream>

#include <cstring>      // Needed for memset

#include <sys/socket.h> // Needed for the socket functions

#include <netdb.h>      // Needed for the socket functions

#include <string.h>

#include <ctype.h>

int main()

{

    int status;

    struct addrinfo host_info;       // The struct that getaddrinfo() fills up with data.

    struct addrinfo *host_info_list; // Pointer to the to the linked list of host_info's.

    memset(&host_info, 0, sizeof host_info);

    std::cout << "Setting up the structs..."  << std::endl;

    host_info.ai_family = AF_UNSPEC;     // IP version not specified. Can be both.

    host_info.ai_socktype = SOCK_STREAM; // Use SOCK_STREAM for TCP or SOCK_DGRAM for UDP.

    host_info.ai_flags = AI_PASSIVE;     // IP Wildcard

    // Now fill up the linked list of host_info structs with google's address information.

    status = getaddrinfo(NULL, "5556", &host_info, &host_info_list);

    // getaddrinfo returns 0 on succes, or some other value when an error occured.

    // (translated into human readable text by the gai_gai_strerror function).

    if (status != 0)  std::cout << "getaddrinfo error" << gai_strerror(status) ;

    std::cout << "Creating a socket..."  << std::endl;

    int socketfd ; // The socket descripter

    socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,

                      host_info_list->ai_protocol);

    if (socketfd == -1)  std::cout << "socket error " ;

    std::cout << "Binding socket..."  << std::endl;

    // we use to make the setsockopt() function to make sure the port is not in use

    // by a previous execution of our code. (see man page for more information)

    int yes = 1;

    status = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

    status = bind(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);

    if (status == -1)  std::cout << "bind error" << std::endl ;

    std::cout << "Listen()ing for connections..."  << std::endl;

    status =  listen(socketfd, 5);

    if (status == -1)  std::cout << "listen error" << std::endl ;

    int new_sd;

    struct sockaddr_storage their_addr;

    socklen_t addr_size = sizeof(their_addr);

    new_sd = accept(socketfd, (struct sockaddr *)&their_addr, &addr_size);

    if (new_sd == -1)

    {

        std::cout << "listen error" << std::endl ;

    }

    else

    {

        std::cout << "Connection accepted. Using new socketfd : "  <<  new_sd << std::endl;

    }

    std::cout << "Waiting to recieve data..."  << std::endl;

    ssize_t bytes_recieved;

    char incomming_data_buffer[1000];

    bytes_recieved = recv(new_sd, incomming_data_buffer,1000, 0);

    // If no data arrives, the program will just wait here until some data arrives.

    if (bytes_recieved == 0) std::cout << "host shut down." << std::endl ;

    if (bytes_recieved == -1)std::cout << "recieve error!" << std::endl ;

    std::cout << bytes_recieved << " bytes recieved :" << std::endl ;

    incomming_data_buffer[bytes_recieved] = '\0';

    

    //Checking String

    std::cout <<"Message: "<<incomming_data_buffer << std::endl;

int i,s1,s2;

s1=0;

char symbol;

char zero = '0';

for(i=0;isdigit(incomming_data_buffer[i]);i++)

{

s1 = s1*10 + (int)incomming_data_buffer[i] - (int)zero;

}

std::cout<<s1<<std::endl;

symbol = incomming_data_buffer[i];

std::cout<<symbol<<std::endl;

i+=1;

s2=0;

for(;isdigit(incomming_data_buffer[i]);i++)

{

s2 = s2*10 + (int)incomming_data_buffer[i] - (int)zero;

}

std::cout<<s2;

int result;

//Doing OPeration

switch(symbol)

{

case '+':

result = s1+s2;

break;

case '-':

result = s1-s2;

break;

case '*':

result = s1*s2;

break;

case '/':

result = s1/s2;

break;

default:

result = 0;

}

//converting to string

char number[100];

int var=0;

for(i=0;result!=0;i++)

{

var = result - ((result/10)*10);

result/=10;

number[i]=(char)var + (char)zero;

}

number[i]='\0';

//reversing string

i=i-1;

int x;

char message[100];

for(x=0;i>=0;i--,x++)

{

message[x] = number[i];

}

message[x] = '\0';

    std::cout << "send()ing back a message..."  << std::endl;

    

    int len;

    ssize_t bytes_sent;

    len = strlen(message);

    bytes_sent = send(new_sd, message, len, 0);

    std::cout << "Stopping server..." << std::endl;

    freeaddrinfo(host_info_list);

    close(new_sd);

    close(socketfd);

return 0 ;

}