-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 13.C
52 lines (43 loc) · 1.24 KB
/
Day 13.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int sockfd, newsockfd, clilen, n;
struct sockaddr_in serv_addr, cli_addr;
char buffer[256];
FILE *fp;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0) {
perror("Error reading from socket");
exit(1);
}
printf("Received request: %s\n", buffer);
fp = fopen(buffer, "rb");
if (fp == NULL) {
printf("Error opening file\n");
exit(1);
}
while ((n = fread(buffer, 1, 256, fp)) > 0) {
write(newsockfd, buffer, n);
}
fclose(fp);
close(newsockfd);
}
return 0;
}