Skip to main content

Simulation of the Ethernet C Project


Download Links :-

http://www.4shared.com/file/COEVNAZ1/cbp.html

http://www.4shared.com/office/RlTAooUV/data.html

http://www.4shared.com/office/m6d563Lk/f-10-p.html

http://www.4shared.com/office/zNz9v5wH/simulation-of-the-ethernet-c-p.html

http://www.4shared.com/file/sZqNjBr-/stationprocess.html

Readme File:

For compiling and executing the project, below are the commands:
Compile CBP file:
g++ -pthread cbp.cpp
Execute:
./a.out 7000   (we can use any port number instead of 7000 here)
Compiling SP file:
gcc  stationprocess.c
Execute:
./a.out  cbpname cbp_port-no file_requested
Eg:          ./a.out cbp 7000 data.txt
To view output: vi slogfile

Simulation Results:

g++ -pthread cbp.cpp
./a.out 7000
Socket created 4
Socket binded
Waiting for data on port….
gcc  stationprocess.c
./a.out cbp 7000 data.txt
Host not foundRead 28 bytes that are [void bal(float amount);
 ]
Read 0 bytes that are []
Total Bytes Read = 28




#include <iostream>
#include<fstream.h>

#include<iostream.h>
#include<sstream>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<sys/time.h>


using namespace std;


pthread_mutex_t slogfile_mutex = PTHREAD_MUTEX_INITIALIZER;

ofstream fout;


void Accept_Connection(int );

void * Process_Request(void * );

void Writing_To_Logfile(std::string msg);


int main(int argc,char* argv[])

{

      int server_port_number;


      if(argc < 2)

      {

            cout<<"./a.out entered in a wrong way need to specify the port numberalong with ./a.out for ex: ./a.out 7000 "<<endl;

            return(0);

      }


      server_port_number = atoi(argv[1]);


      int server_sd=0;

      struct sockaddr_in server_addr;

 

      fout.open("slogfile");


      server_sd = socket(AF_INET,SOCK_STREAM,0); // Creating socket


      if(server_sd<0)

      {

            printf("Cannot create socket");

            return 0;

      }

 

      cout<<"Socket created "<<server_sd<<endl;


      server_addr.sin_family = AF_INET;

    server_addr.sin_addr.s_addr = INADDR_ANY;

    server_addr.sin_port = htons(server_port_number);


      if(bind(server_sd, (struct sockaddr *) &server_addr, sizeof(server_addr))<0) // Binding socket

      {

            cout<<"Cannot bind port "<<endl;

        return 0;

      }


      else

      {

            cout<<"Socket binded "<<endl;

      }


      if (listen(server_sd, 3) < 0)

      {

            cout<<"Error while listening, please try again "<<endl;

            return 0;

      }


      while(1)

      {

            cout<<"Waiting for data on port....  "<<endl;

            Accept_Connection(server_sd);


      }


      fout.close();


}


void Accept_Connection(int server_sd)

{

      int client_sd = 0;

      int client_len = 0;

      struct sockaddr_in client_addr;

      pthread_t * client_processing_thread;


      client_len = sizeof(client_addr);

    client_sd = accept(server_sd, (struct sockaddr *) &client_addr,(socklen_t *) &client_len);


      if(client_sd<0)

      {

            cout<<"Cannot accept connection "<<endl;

        return ;

      }


      else

            cout<<"Connection accepted "<<endl;


      cout<<"Accepting request "<<endl;

      client_processing_thread = new pthread_t;

      pthread_create(client_processing_thread,NULL,Process_Request,(void *)client_sd);


}



void * Process_Request(void * client)

{

      char buffer[1024];

      int BUFFER_SIZE = 1024;

      int client_sd = (int) client;

      int sending_file_length;

      int file_size;

      int elapsed_sec;

      int elapsed_usec;

      double elapsed_time;

      time_t start_time;

      timeval start_send, end_time;

      struct tm * timeinfo;

      bool file_found = true;

      string file_name;

      stringstream storing_data;


      time (&start_time);

      timeinfo = localtime(&start_time);


      ifstream fin; // file being transfered to the client


      memset(buffer, '\0', BUFFER_SIZE);


      if ((read(client_sd,buffer,BUFFER_SIZE)) < 0)

      {

            cout<<"Cannot read the message "<<endl;

            return 0;

      }


      cout<<"File name received: "<<buffer<<endl;

      cout<<"Processing request"<<endl;


      file_name = buffer;

      start_time = time(NULL);

      fin.open(buffer,ios::binary);

      if (!fin.is_open())

      {

            // Eventually this will have to be a write command that will send back to the client, that an error occured.

            fin.open("filenotfound.txt",ios::binary);

            file_found = false;

      }


      fin.seekg (0, ios::end); // get length of file:

      sending_file_length = fin.tellg();

      file_size= sending_file_length;

      fin.seekg (0, ios::beg);


      sleep(((rand() % 5) + 1));

      gettimeofday(&start_send, NULL);


      do

      {

            memset(buffer, '\0', BUFFER_SIZE);  // clear the buffer

            fin.read(buffer, BUFFER_SIZE);


            if (sending_file_length > BUFFER_SIZE)

            {

                  sending_file_length = sending_file_length - BUFFER_SIZE;

                  if((write(client_sd, buffer, BUFFER_SIZE)) < 0)

                        cout<<"Error in sending the message "<<endl;

            }


            else

            {

                  if((write(client_sd, buffer, sending_file_length)) < 0)

                        cout<<"Error in sending the message "<<endl;

            }


      } while (!fin.eof());


      gettimeofday(&end_time, NULL);

      elapsed_sec = end_time.tv_sec - start_send.tv_sec;

      elapsed_usec = end_time.tv_usec - start_send.tv_usec;


      if (elapsed_usec < 0)

      {

            elapsed_sec--;

            elapsed_usec +=1000000;

      }


      elapsed_time = (double)elapsed_sec + (double)elapsed_usec/1000000.0;


      if (file_found == true)

      {

            storing_data<<"File requested : "<<file_name<<"("<<(double)file_size/1000 << "KB) @ "<<asctime(timeinfo)<<file_name<<" transfered in "<<elapsed_time<<"seconds ";

      }


      else

      {

            storing_data<<"File requested : "<<file_name<<" File not found ";

      }


      pthread_mutex_lock(& slogfile_mutex);

      Writing_To_Logfile(storing_data.str());

      pthread_mutex_unlock(& slogfile_mutex);


      close (client_sd);

      fin.close();

    pthread_exit(NULL);


}


void Writing_To_Logfile(string msg)

{

      fout<<msg<<endl<<endl;

}


Popular posts from this blog

C++ Program to find the sum, difference, product and quotient of two integers

#include <iostream.h> #include <conio.h> void main() {   clrscr();   int x = 10;   int y = 2;   int sum, difference, product, quotient;   sum = x + y;   difference = x - y;   product = x * y;   quotient = x / y;   cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;   cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;   cout << "The product of " << x << " & " << y << " is " << product << "." << endl;   cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;   getch(); }

What is Dynamic Memory Allocation in C++ Program

In the computer world, anything that is processed be it an instruction or any data first needs to be loaded and located in internal memory.  In C++ programs also any data that is processed while executing the program is held in the internal memory.  What is Dynamic Memory Allocation? Dynamic Memory allocation means that the memory that will be used during the program is not known beforehand and is allocated dynamically and on the go. It is allocated during the runtime as and when required by the program. In C++ there are two operators used in dynamic memory allocation  1. New  2. Delete New operator in dynamic memory allocation The new operator in C++ is used to create objects of all types. The new operator will allocate memory of the size of the data type specified in the program.  For Example iptr = new int ;  Storing initial values will allocate needed amount of memory from the free store to hold the value of the specified data-type and store the startin...

What is iostream.h in C++ Programing Language ?

In C++ programing language, all header files end with .H extension. In standard C++, all devices are accessed and used as a file.  One such header file is iostream.h in C++ programming language. In this, all input and output devices are treated as files.  Let's quickly look at what are the input and output devices mean in C++ program.  Standard Input Device - Keyboard  Standard Output Device   - Monitor  Standard Error Device - Screen or monitor In C++, input and output are not defined within, and are implemented with the help of a component of the C++ standard library which is I/O library.  A file is read simply as a stream of bytes at the lowest level. But at a user level, a file consists of possibly mixed data types which can be characters, arithmetic values class, objects etc.  What are the predefined streams in I/O Library? As discussed above there are input, output and error library in c++, they have some predefined streams objects as well w...