-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
59 lines (44 loc) · 1.31 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void receive_file(int socket) {
FILE *file;
char buffer[BUFFER_SIZE];
int bytes_received;
file = fopen("received_file.extension", "wb");
if (file == NULL) {
perror("File open error");
exit(1);
}
while ((bytes_received = recv(socket, buffer, BUFFER_SIZE, 0)) > 0) {
fwrite(buffer, 1, bytes_received, file);
}
fclose(file);
}
int main() {
int client_socket;
struct sockaddr_in server_addr;
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
receive_file(client_socket);
close(client_socket);
return 0;
}