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
]
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;
}