Add an option to limit the number of packet sent.
This commit is contained in:
parent
6fd2a5e47b
commit
709c2f01a1
1 changed files with 21 additions and 9 deletions
30
echoc.c
30
echoc.c
|
@ -36,13 +36,12 @@ struct sockaddr *server;
|
||||||
socklen_t serverlen;
|
socklen_t serverlen;
|
||||||
unsigned int seq = 0;
|
unsigned int seq = 0;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
errx(2, "usage: echoc [-i ms][-t ms][-v] server");
|
errx(2, "usage: echoc [-c nbr][-i ms][-t ms][-v] server");
|
||||||
}
|
}
|
||||||
|
static void
|
||||||
static void
|
|
||||||
send_packet(int unused)
|
send_packet(int unused)
|
||||||
{
|
{
|
||||||
if (sendto(sock, &seq, sizeof(seq), 0, server,
|
if (sendto(sock, &seq, sizeof(seq), 0, server,
|
||||||
|
@ -74,11 +73,16 @@ main(int argc, char *argv[])
|
||||||
int nfds, received = 0;
|
int nfds, received = 0;
|
||||||
int error, buffer, last = -1;
|
int error, buffer, last = -1;
|
||||||
int disconnected;
|
int disconnected;
|
||||||
|
int we_count = 0, counter; /* don't loop forever */
|
||||||
extern int optind;
|
extern int optind;
|
||||||
|
|
||||||
setbuf(stdout, NULL);
|
setbuf(stdout, NULL);
|
||||||
while ((ch = getopt(argc, argv, "i:t:v")) != -1) {
|
while ((ch = getopt(argc, argv, "c:i:t:v")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
|
case 'c':
|
||||||
|
we_count++;
|
||||||
|
counter = atoi(optarg);
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
interval = atoi(optarg);
|
interval = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
@ -99,7 +103,8 @@ main(int argc, char *argv[])
|
||||||
|
|
||||||
if (interval <= 0 || timeout <= 0)
|
if (interval <= 0 || timeout <= 0)
|
||||||
errx(2, "interval and timeout must be > 0");
|
errx(2, "interval and timeout must be > 0");
|
||||||
|
if (counter < 2)
|
||||||
|
errx(2, "can't count down from nothing");
|
||||||
/* force timeout >= interval */
|
/* force timeout >= interval */
|
||||||
if (timeout < interval) {
|
if (timeout < interval) {
|
||||||
timeout = interval;
|
timeout = interval;
|
||||||
|
@ -205,6 +210,13 @@ main(int argc, char *argv[])
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf("received %d %ld.%06ld\n", buffer,
|
printf("received %d %ld.%06ld\n", buffer,
|
||||||
(long)diff.tv_sec, diff.tv_usec);
|
(long)diff.tv_sec, diff.tv_usec);
|
||||||
|
if (we_count) {
|
||||||
|
counter--;
|
||||||
|
if (counter == 0) {
|
||||||
|
printf("all job done\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close(sock);
|
close(sock);
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue