File: udpblast.c

package info (click to toggle)
netdiag 1.1-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,544 kB
  • ctags: 2,166
  • sloc: ansic: 23,629; sh: 5,544; makefile: 329; awk: 130
file content (112 lines) | stat: -rw-r--r-- 2,202 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>

#include <netinet/in.h>

#include <netdb.h>
#include <stdio.h>

#define BLKSIZE (1024*32)

struct	sockaddr_in sin;
struct	servent *sp;
struct	hostent *host;

unsigned long starts, startms, stops, stopms, expms;
struct timeval ti; 
struct timezone tiz;

char 	greet[BLKSIZE] = "Hi!";
int 	nblocks;
int	f;

int
main(argc, argv)
     int argc;
     char **argv;
{
	register int i;

	if (argc!=3)
	{
		fprintf(stderr, "usage: tcpblast destination nblkocks\n");
		fprintf(stderr, "blocksize: %d bytes\n", BLKSIZE);
		exit(1);
	}

	nblocks = atoi(argv[2]);
	if (nblocks<=1 || nblocks>=10000)
	{
		fprintf(stderr, "tcpblast: 1 < nblocks <= 10000 \n");
		exit(1);
	}

	bzero((char *)&sin, sizeof (sin));
	sin.sin_family = AF_INET;
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f < 0) {
		perror("tcpblast: socket");
		exit(3);
	}
	{
	  int siz = 1024*32;
	  setsockopt(f, SOL_SOCKET, SO_SNDBUF, &siz, sizeof(siz));
	  getsockopt(f, SOL_SOCKET, SO_SNDBUF, &siz, sizeof(siz));
	  printf("SO_SNDBUF = %d\n", siz);
	}
	if (bind(f, &sin, sizeof (sin)) < 0) {
		perror("tcpblast: bind");
		exit(1);
	}

	host = gethostbyname(argv[1]);
	if (host) {
		sin.sin_family = host->h_addrtype;
		bcopy(host->h_addr, &sin.sin_addr, host->h_length);
	} else {
		sin.sin_family = AF_INET;
		sin.sin_addr.s_addr = inet_addr(argv[1]);
		if (sin.sin_addr.s_addr == -1) {
			fprintf(stderr, "tcpblast: %s unknown host\n", argv[1]);
			exit(1);
		}
	}
	sin.sin_port = htons(9);

	if (connect(f, &sin, sizeof(sin)) <0)
	{
		perror("tcpblast connect:");
		exit(1);
	}

	if (gettimeofday(&ti, &tiz) < 0)
	{
		perror("tcpblast time:");
		exit(1);
	}
	starts  = ti.tv_sec;
	startms = ti.tv_usec / 1000L;


	for (i=0; i<nblocks; i++)
	{
		if (write(f, greet, BLKSIZE) != BLKSIZE)
			perror("tcpblast send:");
		write(1, ".", 1);
	}

	if (gettimeofday(&ti, &tiz) < 0)
	{
		perror("tcpblast time:");
		exit(1);
	}
	stops  = ti.tv_sec;
	stopms = ti.tv_usec / 1000L;

	expms = (stops-starts)*1000 + (stopms-startms);
	printf("\n%d KB in %ld msec", (nblocks*BLKSIZE)/1024, expms);
	printf("  =  %.1f kbit/s", (nblocks*BLKSIZE*8.0)/expms);
	printf("\n");
}