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

Program of virtual piano

//////////////Tested And Created By C++/////////////////////////////// #include<stdio.h> #include<dos.h> #include<conio.h> #include<stdlib.h> #define SHOW 1 #define HIDE 2 union REGS input,output; class piano {  public:int BIGKEY,MIDKEY,back,border;     piano()//init constructor     {         BIGKEY=15;         MIDKEY=1;         back=7;         border=15;     } }color; void drawpiano(int x,int y); int check_xy(int x,int y); void BOX(int c,int r,int c1,int r1,int col); int initmouse(); void setupscreen(); void pointer(int on); void restrictmouse(int x1,int y1,int x2,int y2); void check_keys(int x,int y); void getmouse(int *button,int *x,int *y); float freq[7] = {130.81, 146.83, 164.81, 174.61,196, 220, 246.94 } ; int n=0,a=4,backcolor=2,exitcode=1; void showbar(int t) {  if(t>65) t=65;  if(t<1) t=1;  textcolor(15);  for(int q=0;q<=t;t++)  {     gotoxy(3+q,4);     cprintf("Û");  } } void main() {  int

Putimage function in c

putimage function outputs a bit image onto the screen. Declaration:- void putimage(int left, int top, void *ptr, int op); putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include<graphics.h> #include<conio.h> #include<stdlib.h>   main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p;   initgraph(&gd,&gm,"C:\\TC\\BGI");   setcolor(YELLOW);