-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_client.cc
126 lines (108 loc) · 2.37 KB
/
udp_client.cc
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
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include "minet_socket.h"
#define BUFLEN 15000
#define GENERATE 0
using std::cout;
using std::cerr;
using std::endl;
void usage()
{
cerr << "udp_client k|u host port\n";
}
int main(int argc, char *argv[])
{
char buf[BUFLEN];
sockaddr_in server_sa, client_sa;
struct hostent *he;
int fd, rc;
if (argc!=4) {
usage();
exit(-1);
}
if (toupper(argv[1][0])=='K') {
cerr << "Using kernel stack.\n";
if (minet_init(MINET_KERNEL)<0) {
cerr << "Stack initialization failed.\n";
goto err;
} else {
cerr << "Stack initialized.\n";
}
} else {
cerr << "Using Minet User Level Stack.\n";
if (minet_init(MINET_USER)<0) {
cerr << "Stack initialization failed.\n";
goto err;
} else {
cerr << "Stack initialized.\n";
}
}
cerr << "Stack initialized.\n";
he = gethostbyname(argv[2]);
if (he==0) {
cerr << "Unknown host.\n";
goto err;
}
bzero(&server_sa,sizeof(server_sa));
server_sa.sin_family=AF_INET;
memcpy((void*)(&(server_sa.sin_addr)),he->h_addr,he->h_length);
server_sa.sin_port=htons(atoi(argv[3]));
fd = minet_socket(SOCK_DGRAM);
if (fd<0) {
cerr << "Can't create socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket created.\n";
}
bzero(&client_sa,sizeof(client_sa));
client_sa.sin_family=AF_INET;
client_sa.sin_addr.s_addr=htonl(INADDR_ANY);
client_sa.sin_port=htons(0);
if (minet_bind(fd,&client_sa)<0) {
cerr << "Can't bind socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket bound.\n";
}
if (minet_connect(fd,&server_sa)<0) {
cerr << "Can't connect socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket connected.\n";
}
cout << "Enter text to send.\n";
while (1) {
#if GENERATE
sleep(1);
for (int i=0;i<BUFLEN;i++) {
buf[i]='a'+i%26;
}
buf[BUFLEN-1]='\n';
rc=BUFLEN;
#else
if ((rc=read(fileno(stdin),buf,BUFLEN))<0) {
cerr << "Read failed.\n";
minet_perror("reason:");
goto err;
}
#endif
if (rc==0) {
cerr << "Done.\n";
goto done;
}
if (minet_write(fd,buf,rc)<0) {
cerr << "Write failed.\n";
minet_perror("reason:");
goto err;
}
}
done:
err:
minet_close(fd);
minet_deinit();
return 0;
}