📄 client.c.txt
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <signal.h>
#include <wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#define SRVRPORT 3000
#define BUFSIZE 4096
void reaper(int sig)
{
int status;
while(wait3(&status, WNOHANG, (struct rusage *)0) >= 0); //wait3 is used to cleanup all child process as opposed to wait - which cleans up only one child.
}
int main(int argc, char **argv)
{
int sockd, c,p,q,conncount,clinst,n=0;
struct sockaddr_in sin;
char buf[BUFSIZE];
char *filename,*servername;
pid_t pid=1;
struct timeval tval;
double start_ttime, end_ttime;
struct hostent *hostp;
if(argc != 5) {
printf("Usage: %s < filename> <servername > <client instances> <connections> \n", argv[0]);
}
switch(argc)
{
case 5:
conncount = atoi(argv[4]);
clinst = atoi(argv[3]);
servername = argv[2];
filename = argv[1];
break;
case 4:
clinst = atoi(argv[3]);
servername = argv[2];
filename = argv[1];
conncount = 5;
break;
case 3:
servername = argv[2];
filename = argv[1];
clinst = 5;
conncount = 5;
break;
case 2:
filename = argv[1];
servername = "localhost";
clinst = 5;
conncount = 5;
break;
default:
printf("Usage: %s < filename> <servername > <client instances> <connections> \n", argv[0]);
exit(0);
}
strcpy(servername, argv[2]);
printf("File that is being read by the client is %s\n",filename);
printf("Number of instances in the client is %d\n",clinst);
printf("Number of connections per instance in this client is %d\n",conncount);
gettimeofday(&tval,NULL);
start_ttime = tval.tv_sec + tval.tv_usec/100000.0;
for (p = 0; p <= clinst-1; p++,n=0)
{
if(pid !=0){
if((pid = fork()) == 0)
{
/* Starting time */
for (q = 1; q <= conncount; q++)
{
sockd = socket(AF_INET,SOCK_STREAM,0);
if(sockd <= 0) {
perror("socket");
exit(-1);
}
sin.sin_family = AF_INET;
sin.sin_port = htons(SRVRPORT);
sin.sin_addr.s_addr = INADDR_ANY;
hostp = gethostbyname(servername);
if(hostp == (struct hostent *)NULL)
{
printf("HOST NOT FOUND --> ");
/* h_errno is usually defined in netdb.h */
printf("---This is a client program---\n");
printf("Command usage: %s <server name or IP>\n", argv[0]);
close(sockd);
exit(-1);
}
memcpy(&sin.sin_addr, hostp->h_addr, sizeof(sin.sin_addr));
c = connect(sockd, (struct sockaddr *)&sin, sizeof(sin));
if( c == -1) {
perror("connect");
exit(-1);
}
int i;
i = write(sockd, filename, strlen(filename));
int k,totalsize = 0;
while ((k = read(sockd,buf,sizeof(buf))) > 0 ) {
printf("Number of bytes read %d\n", k);
totalsize += k;
}
printf("Total number of bytes read for %d instance and %d is%d\n", p+1, q,totalsize);
if (k < 0) {
perror("read");
exit(0);
}
//close(c);
}
// gettimeofday(&tval,NULL);
// end_time = tval.tv_sec + tval.tv_usec/100000.0;
// n=n + abs((end_time-start_time)*1000);
/* Printing the output : Time taken in ms */
}
}
}
gettimeofday(&tval,NULL);
end_ttime = tval.tv_sec + tval.tv_usec/100000.0;
printf("Total time for %d instance and %d connection is %d\n",p,conncount,abs((end_ttime-start_ttime)*1000));
reaper(-1);
exit(1);
//return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -