-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
86 lines (67 loc) · 2.44 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
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP argv[1] // Replace with server IP
#define SERVER_PORT argv[2] // Replace with server port
#define BUFFER_SIZE 1024 // Size of the chunks we will split the file up into
int main(int argc, char* argv[]) {
setbuf(stdout, NULL);
if(argc < 4) {
printf("Usage:\n%s <server IP> <server port> <output file name or path>\n", argv[0]);
return 1;
} // If there's less than 4 arguments, we print the usage text.
// Define the variable that will be used for the client socket
int client_socket;
struct sockaddr_in server_address;
// Create the client socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Convert the port argument into an integer
int port = atoi(SERVER_PORT);
// Set up server address
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
if (inet_pton(AF_INET, SERVER_IP, &(server_address.sin_addr)) <= 0) {
perror("Invalid address or address not supported");
exit(EXIT_FAILURE);
}
// Connect to the server
if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
// Open the file
FILE* file;
file = fopen(argv[3], "w");
if (file == NULL) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
// Receive the file from the server
char buffer[BUFFER_SIZE];
ssize_t numBytesReceived;
size_t bytesReceivedSoFar = 0;
// Keep receiving shit until we reach the amount of bytes we need to receive, constantly printing the progress
while ((numBytesReceived = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
fwrite(buffer, sizeof(char), numBytesReceived, file);
bytesReceivedSoFar += numBytesReceived;
printf("Received %ld megabytes so far", bytesReceivedSoFar / 1000000);
printf("\r");
}
if (numBytesReceived < 0) {
perror("File receiving failed");
exit(EXIT_FAILURE);
}
printf("\rReceived %zu megabytes from the server.\n", bytesReceivedSoFar / 1000000);
// Close the file
fclose(file);
// Close the socket
close(client_socket);
return 0;
}