-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (62 loc) · 1.83 KB
/
main.cpp
File metadata and controls
71 lines (62 loc) · 1.83 KB
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
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <cstring>
#include <iostream>
#include "coroutine.h"
using namespace coroutine;
int start(int port) {
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
int addrlen = sizeof(address);
int fd = socket(AF_INET, SOCK_STREAM, 0);
bind(fd, (struct sockaddr *)&address, sizeof(address));
return fd;
}
void echo(void *data) {
int sock = *(int*) data;
char buffer[1024] = {0};
const char *hello = "Hello from server\n";
const char *bye = "GoodBye from server\n";
printf("start connection with %d\n", sock);
send(sock, hello, strlen(hello), 0);
while (1) {
int len = co_recv(sock , buffer, 1024, 0);
if (len <= 0) break;
buffer[len] = 0;
if (strcmp(buffer, "quit\n") == 0) {
printf("quit received from %d\n", sock);
send(sock, bye, strlen(bye), 0);
break;
}
printf("receive from %d : %s", sock, buffer);
send(sock, buffer, strlen(buffer), 0);
}
printf("close connection with %d\n", sock);
close(sock);
}
void server(void *params) {
int port = *(int *)params;
char buf[1024];
int listens = start(port);
listen(listens, 3);
while (1) {
struct sockaddr_in address;
bzero(&address, sizeof(address));
int addrlen;
int client_s = co_accept(listens, (struct sockaddr *)&address, (socklen_t*)&addrlen);
go(echo, new int(client_s));
}
}
int main(int argc, const char** argv) {
int port = 8080;
if (argc > 1) port = atoi(argv[1]);
go(server, new int(port));
Schedule::instance()->run();
return 0;
}