-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
115 lines (92 loc) · 3.1 KB
/
client.c
File metadata and controls
115 lines (92 loc) · 3.1 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
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <ctype.h>
//declare variables (serverIP, serverPort, buffer for sending/receiving data)
#define SERVER_IP "192.168.1.99"
#define PORT 8080
#define BUFFER_SIZE 1024
// Helper function to trim leading/trailing whitespace from a string
void trim_whitespace(char *str) {
char *end;
while(isspace((unsigned char)*str)) str++;
if(*str == 0)
return;
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
*(end+1) = 0;
}
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE] = {0};
ssize_t bytes_received;
// Create socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("Error in socket creation");
exit(EXIT_FAILURE);
}
// Configure server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
// Convert IP address from string to binary form
if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
perror("Invalid address/Address not supported");
close(client_socket);
exit(EXIT_FAILURE);
}
// Connect to the server
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Error in connecting to server");
close(client_socket);
exit(EXIT_FAILURE);
}
printf("Connected to the banking server at %s:%d\n", SERVER_IP, PORT);
printf("Available commands: OPEN, CLOSE, WITHDRAW, DEPOSIT, BALANCE, STATEMENT, QUIT\n");
// Send requests and receive responses
while (1) {
printf("> ");
// Get command from user
if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) {
perror("Error reading input");
break;
}
// Remove newline and whitespace
buffer[strcspn(buffer, "\n")] = 0;
trim_whitespace(buffer);
// Check if the user wants to quit
if (strcmp(buffer, "QUIT") == 0) {
send(client_socket, buffer, strlen(buffer), 0);
bytes_received = read(client_socket, buffer, BUFFER_SIZE - 1);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
printf("%s", buffer);
}
break;
}
// Send the command to the server
if (send(client_socket, buffer, strlen(buffer), 0) < 0) {
perror("Error sending data");
break;
}
// Clear the buffer for receiving
memset(buffer, 0, BUFFER_SIZE);
// Receive the response from the server
bytes_received = read(client_socket, buffer, BUFFER_SIZE - 1);
if (bytes_received <= 0) {
printf("Server disconnected.\n");
break;
}
// print response
buffer[bytes_received] = '\0';
printf("%s", buffer);
}
// Close the client socket
close(client_socket);
printf("Client disconnected.\n");
return 0;
}