extend BSD socket examples (create more messages for a stress test)
This commit is contained in:
parent
ce4bfa61bd
commit
e5ac848901
2 changed files with 34 additions and 3 deletions
|
@ -12,10 +12,14 @@
|
||||||
#undef errno
|
#undef errno
|
||||||
extern int errno;
|
extern int errno;
|
||||||
|
|
||||||
|
#define QUIT 0
|
||||||
|
#define S2C 1
|
||||||
#define MAX_BUF 100
|
#define MAX_BUF 100
|
||||||
|
#define N 10
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
int i, cmd;
|
||||||
int sockd;
|
int sockd;
|
||||||
int count;
|
int count;
|
||||||
struct sockaddr_in serv_name;
|
struct sockaddr_in serv_name;
|
||||||
|
@ -48,10 +52,24 @@ int main(int argc, char* argv[])
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
count = read(sockd, buf, MAX_BUF);
|
for(i=0, cmd=S2C; i<N; i++) {
|
||||||
write(1, buf, count);
|
// request new message
|
||||||
|
write(sockd, &cmd, sizeof(int));
|
||||||
|
|
||||||
|
// dump current value of i
|
||||||
|
snprintf(buf, MAX_BUF, "%d. ", i);
|
||||||
|
write(1, buf, strlen(buf));
|
||||||
|
|
||||||
|
// read new message and dump the incoming message on the screen
|
||||||
|
count = read(sockd, buf, MAX_BUF);
|
||||||
|
write(1, buf, count);
|
||||||
|
}
|
||||||
|
cmd = QUIT;
|
||||||
|
write(sockd, &cmd, sizeof(int));
|
||||||
|
|
||||||
close(sockd);
|
close(sockd);
|
||||||
|
|
||||||
|
printf("Leave client...\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,16 @@ extern int errno;
|
||||||
|
|
||||||
static char msg[] =" Hello from server!\n";
|
static char msg[] =" Hello from server!\n";
|
||||||
|
|
||||||
|
#define QUIT 0
|
||||||
|
#define S2C 1
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
int sockd, sockd2;
|
int sockd, sockd2;
|
||||||
unsigned int addrlen;
|
unsigned int addrlen;
|
||||||
struct sockaddr_in my_name, peer_name;
|
struct sockaddr_in my_name, peer_name;
|
||||||
int status;
|
int status;
|
||||||
|
int cmd, i=0;
|
||||||
|
|
||||||
/* create a socket */
|
/* create a socket */
|
||||||
sockd = socket(AF_INET, SOCK_STREAM, 0);
|
sockd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -64,7 +68,16 @@ int main(int argc, char* argv[])
|
||||||
perror("Wrong connection");
|
perror("Wrong connection");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
write(sockd2, msg, strlen(msg)+1);
|
|
||||||
|
again:
|
||||||
|
read(sockd2, &cmd, sizeof(cmd));
|
||||||
|
printf("server got %d. command: %d\n", i, cmd);
|
||||||
|
i++;
|
||||||
|
if (cmd == S2C)
|
||||||
|
write(sockd2, msg, strlen(msg)+1);
|
||||||
|
if (cmd != QUIT)
|
||||||
|
goto again;
|
||||||
|
|
||||||
close(sockd2);
|
close(sockd2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue