-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
38 lines (32 loc) · 853 Bytes
/
server.c
File metadata and controls
38 lines (32 loc) · 853 Bytes
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
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(8080),
.sin_addr.s_addr = INADDR_ANY
};
bind(s, (struct sockaddr *)&addr, sizeof(addr));
listen(s, 8);
while (1) {
char buffer[265] = {0};
int client_fd = accept(s, 0, 0);
recv(client_fd, buffer, 256, 0);
printf("%s", buffer);
send(client_fd, buffer, 256, 0);
if (strncmp(buffer, "quit", 4) == 0) {
send(client_fd, "Goodbye...\n", 11, 0);
close(client_fd);
break;
} else {
close(client_fd);
}
}
printf("Goodbye...\n");
close(s);
return 0;
}