-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitclient2.c
More file actions
97 lines (78 loc) · 2.41 KB
/
Copy pathitclient2.c
File metadata and controls
97 lines (78 loc) · 2.41 KB
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
struct sockaddr_in serv_addr;
int skfd;
ssize_t r, w;
unsigned short serv_port = 25020; // port number used by the server
char serv_ip[] = "127.0.0.1"; // server IP Address
void trim_newline(char *s) {
size_t i = strlen(s);
while (i > 0 && (s[i-1] == '\n' || s[i-1] == '\r')) {
s[i-1] = '\0';
--i;
}
}
int main() {
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(serv_port);
inet_aton(serv_ip, &serv_addr.sin_addr);
printf("\nTCP ECHO CLIENT\n");
if ((skfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("CLIENT ERROR: Cannot create socket");
exit(1);
}
if (connect(skfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("CLIENT ERROR: Cannot connect to the server");
close(skfd);
exit(1);
}
printf("\nCLIENT: Connected to the server.\n");
for (;;) {
char sbuff[256];
char rbuff[256];
printf("Enter Client's message:\n");
fflush(stdout);
if (fgets(sbuff, sizeof(sbuff), stdin) == NULL) {
printf("No input (stdin closed). Exiting.\n");
break;
}
trim_newline(sbuff);
printf("\nCLIENT: Message to the server: %s\n", sbuff);
/* send only the meaningful bytes */
w = write(skfd, sbuff, strlen(sbuff));
if (w < 0) {
perror("CLIENT ERROR: Cannot send message to the server");
close(skfd);
exit(1);
}
if (strcmp(sbuff, "STOP") == 0) {
printf("Stopping the connection as client sent STOP.\n");
break;
}
/* read server reply */
r = read(skfd, rbuff, sizeof(rbuff) - 1);
if (r < 0) {
perror("CLIENT ERROR: Cannot receive message from the server");
break;
} else if (r == 0) {
printf("Server closed connection.\n");
break;
}
rbuff[r] = '\0';
trim_newline(rbuff);
printf("\nCLIENT: Message from the server: %s\n", rbuff);
if (strcmp(rbuff, "STOP") == 0) {
printf("Stopping the connection as server sent STOP.\n");
break;
}
}
close(skfd);
return 0;
}