-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogClient.c
More file actions
111 lines (81 loc) · 3.6 KB
/
logClient.c
File metadata and controls
111 lines (81 loc) · 3.6 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h>
#include <sys/socket.h> /* for socket(), bind(), connect() */
#include <sys/types.h>
#include <unistd.h> /* for close() */
#include <netinet/in.h>
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#define BUFFSIZE 100
int main(int argc, char *argv[])
{
/* Variables declarations */
int sock_fd; // socket file descriptor for the client
struct sockaddr_in target_addr; // address of the server we are going to connect to
struct in_addr inp; // structure used to store the IPv4 address in binary form
char *serverIP; // IP address of the server
unsigned short serverPort; // port number on which the server is listening
char msgToSend[BUFFSIZE]; // array to store the message to send to the server
/* Check correct number of arguments */
if (argc != 3) {
fprintf(stderr, "Usage: %s <IP_address> <listening_port>\n", argv[0]);
exit(1);
}
serverIP = argv[1]; // first argument
serverPort = atoi(argv[2]); // second argument
/* Check if the IPv4 address (dotted-notation) given by the user is valid */
if (inet_aton(serverIP, &inp) == 0) {
perror("IPv4 address not valid");
exit(1);
}
/**********************************************************************************/
/* (1) Socket creation */
// Creates a socket specifying domain, type and protocol. Returns a descriptor.
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket() failed");
exit(1);
}
/**********************************************************************************/
/* (2) Connection to the server socket */
// Construct the server address structure
target_addr.sin_family = AF_INET; // Internet address familiy
target_addr.sin_addr.s_addr = inet_addr(serverIP); // Server IP address: from numbers-and-dots notation to binary data
target_addr.sin_port = htons(serverPort); // Server port number
memset(&(target_addr.sin_zero), '\0', 8); // Zero the rest of the struct
// Attempts to connect sock_fd to another socket specified by target_addr
if (connect(sock_fd, (struct sockaddr *) &target_addr, sizeof(target_addr)) < 0) {
perror("connect() failed");
exit(1);
}
printf("Connected to the server.\n");
/**********************************************************************************/
/* (3) Send/receive data */
/* Ask the user to insert the message to send to the log server */
while (1) {
printf("Enter a message to send to the log server (type 'exit' to quit): ");
// fgets() reads also the newline character (\n) at the end when we press enter
if (fgets(msgToSend, BUFFSIZE, stdin) == NULL) {
perror("error with fgets()");
exit(1);
}
// Remove the newline character ('\n') at the end of the string and replace it with the null terminator ('\0')
if (msgToSend[strlen(msgToSend) - 1] == '\n') msgToSend[strlen(msgToSend) - 1] = '\0';
// if the user types 'exit' we exit from the loop and send a request to the server to close the connection
if (strcmp(msgToSend, "exit") == 0) {
printf("Closing the connection...\n");
send(sock_fd, "CLOSE_CONNECTION", 16, 0);
break; // exit from while loop
}
printf("The entered message is: %s\n\n", msgToSend);
// Send the message to the server
if (send(sock_fd, msgToSend, strlen(msgToSend), 0) != strlen(msgToSend)) {
perror("A different number of bytes was sent by send()!");
exit(1);
}
}
/**********************************************************************************/
/* (4) Terminate the connection */
close(sock_fd);
printf("Disconnected from the server.\n");
return 0;
}