-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.txt
58 lines (48 loc) · 1.48 KB
/
client.txt
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
// Create the client socket
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// Set the server address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
// Connect to the server
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}
printf("Enter the value of n for the Fibonacci sequence: ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = '\0'; // Remove the newline character
// Send the value of n to the server
if (write(client_fd, buffer, strlen(buffer) + 1) < 0) {
perror("write");
exit(EXIT_FAILURE);
}
// Read the response from the server
memset(buffer, 0, BUFFER_SIZE);
if (read(client_fd, buffer, BUFFER_SIZE) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
printf("%s\n", buffer);
// Close the client socket
close(client_fd);
return 0;
}