-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.c
98 lines (77 loc) · 2.38 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "ascii.h"
#include "client.h"
#include "options.h"
int sockfd = 0;
void sigint_handler(int sigint) {
(void) (sigint);
ascii_write_destroy();
printf("Closing connection and exiting . . .\n");
close(sockfd);
exit(0);
}
int main(int argc, char *argv[]) {
options_init(argc, argv);
char *address = opt_address;
int port = strtoint(opt_port);
char recvBuff[40000];
struct sockaddr_in serv_addr;
struct timespec
sleep_start = {
.tv_sec = 3,
.tv_nsec = 0
},
sleep_stop = {
.tv_sec = 0,
.tv_nsec = 0
};
// Cleanup nicely on Ctrl+C.
signal(SIGINT, sigint_handler);
/* read from the socket as long as the size of the read is > 0 */
while(1) {
// try to open a socket
memset(recvBuff, '0',sizeof(recvBuff));
// error creating socket
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Error: could not create socket \n");
return 1;
}
// reserve memory space to store IP address
memset(&serv_addr, '0', sizeof(serv_addr));
// set type of address to IPV4 and port to 5000
printf("Connecting on port %d\n", port);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
// an error occurred when trying to set server address and port number
if(inet_pton(AF_INET, address, &serv_addr.sin_addr) <= 0) {
printf("\n Error: inet_pton \n");
return 1;
}
int read_result = 0;
ascii_write_init();
printf("Attempting to connect...\n");
// failed when trying to connect to the server
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\n Error: connect failed \n");
}
while (0 < (read_result = read(sockfd, recvBuff, sizeof(recvBuff)-1))) {
recvBuff[read_result] = 0;
ascii_write(recvBuff);
}
console_clear();
nanosleep((struct timespec *)&sleep_start,(struct timespec *)&sleep_stop);
}
return 0;
}