4 * lwp (echo) connection handler server
10 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
22 struct sockaddr_in addr;
27 int readConn(argc, argv, ud)
32 struct context *ctx = (struct context *) ud;
38 lwpSleepFd(ctx->fd, LWP_FD_READ);
39 printf("waiting to read\n");
40 if ((n = read(ctx->fd, buf, sizeof(buf))) <= 0)
42 printf("got %d char\n", n);
43 lwpSleepFd(ctx->fd, LWP_FD_WRITE);
44 printf("waiting to write\n");
45 if (write(ctx->fd, buf, n) < 0)
47 printf("wrote %d char\n", n);
49 printf("process/fd %d exiting\n", ctx->fd);
55 int acceptConn(argc, argv)
59 struct sockaddr_in sin;
67 fprintf(stderr, "Usage: %s port\n", *argv);
70 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
71 perror("inet socket");
74 sin.sin_family = AF_INET;
75 sin.sin_port = htons(atoi(argv[1]));
76 sin.sin_addr.s_addr = 0;
77 if (bind(s, &sin, sizeof(sin)) < 0) {
78 perror("inet socket bind");
81 if (listen(s, LISTENMAXCONN) < 0) {
82 perror("inet socket listen");
85 maxfd = getdtablesize() - 1;
87 lwpSleepFd(s, LWP_FD_READ);
89 ns = accept(s, &sin, &len);
95 fprintf(stderr, "no more connections");
98 printf("got connection from %s\n", inet_ntoa(sin.sin_addr));
99 ctx = (struct context *) malloc(sizeof(*ctx));
102 ctx->us = lwpCreate(SP_READER, readConn, 8192, 0, 0, ctx);
112 lwpCreate(SP_ACCEPT, acceptConn, 8192, argc, argv, 0);