-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcsapp-client.c
85 lines (71 loc) · 1.85 KB
/
rcsapp-client.c
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
/* Mahesh V. Tripunitara
* University of Waterloo
* rcsapp-client.c -- takes as cmd line args a server ip & port.
* After establishing a connection, reads from stdin till eof
* and sends everything it reads to the server. sleep()'s
* occasionally in the midst of reading & sending.
*/
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "rcs.h"
unsigned int getrand() {
int f = open("/dev/urandom", O_RDONLY);
if(f < 0) {
perror("open(/dev/urandom)"); return 0;
}
unsigned int ret;
read(f, &ret, sizeof(unsigned int));
close(f);
return ret;
}
int main(int argc, char *argv[]) {
if(argc < 3) {
printf("usage: %s <server-ip> <server-port>\n", argv[0]);
exit(0);
}
int s = rcsSocket();
fprintf(stderr, "Inside client main, the socket index is %d\n", s);
struct sockaddr_in a;
memset(&a, 0, sizeof(struct sockaddr_in));
a.sin_family = AF_INET;
a.sin_port = 0;
a.sin_addr.s_addr = INADDR_ANY;
if(rcsBind(s, (struct sockaddr_in *)(&a)) < 0) {
perror("bind"); exit(1);
}
if(rcsGetSockName(s, &a) < 0) {
fprintf(stderr, "rcsGetSockName() failed. Exiting...\n");
exit(0);
}
unsigned char buf[256];
int nread = -1;
a.sin_family = AF_INET;
a.sin_port = htons((uint16_t)(atoi(argv[2])));
if(inet_aton(argv[1], &(a.sin_addr)) < 0) {
fprintf(stderr, "inet_aton(%s) failed.\n", argv[1]);
exit(1);
}
if(rcsConnect(s, (struct sockaddr_in *)(&a)) < 0) {
perror("connect"); exit(1);
}
while((nread = read(STDIN_FILENO, buf, 256)) > 0) {
if(rcsSend(s, buf, nread) < 0) {
perror("send"); exit(1);
}
sleep(getrand()%7);
}
fprintf(stderr, "%s\n", "Client shutting down.....");
rcsClose(s);
return 0;
}