-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
74 lines (58 loc) · 1.8 KB
/
server.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void send_file(int client_socket, str path) {
FILE *file;
char buffer[BUFFER_SIZE];
int bytes_read;
file = fopen("Makefile2", "rb");
if (file == NULL) {
perror("File open error");
exit(1);
}
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
if (send(client_socket, buffer, bytes_read, 0) == -1) {
perror("Send error");
exit(1);
}
}
fclose(file);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int opt = 1;
int addrlen = sizeof(server_addr);
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("Setsockopt failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_socket, 3) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, (socklen_t *)&addrlen)) < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
send_file(client_socket);
close(client_socket);
close(server_socket);
return 0;
}