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;
28 readConn(argc, argv, ud)
33 struct context *ctx = (struct context *)ud;
39 lwpSleepFd(ctx->fd, LWP_FD_READ);
40 printf("waiting to read\n");
41 if ((n = read(ctx->fd, buf, sizeof(buf))) <= 0)
43 printf("got %d char\n", n);
44 lwpSleepFd(ctx->fd, LWP_FD_WRITE);
45 printf("waiting to write\n");
46 if (write(ctx->fd, buf, n) < 0)
48 printf("wrote %d char\n", n);
50 printf("process/fd %d exiting\n", ctx->fd);
57 acceptConn(argc, argv)
61 struct sockaddr_in sin;
69 fprintf(stderr, "Usage: %s port\n", *argv);
72 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
73 perror("inet socket");
76 sin.sin_family = AF_INET;
77 sin.sin_port = htons(atoi(argv[1]));
78 sin.sin_addr.s_addr = 0;
79 if (bind(s, &sin, sizeof(sin)) < 0) {
80 perror("inet socket bind");
83 if (listen(s, LISTENMAXCONN) < 0) {
84 perror("inet socket listen");
87 maxfd = getdtablesize() - 1;
89 lwpSleepFd(s, LWP_FD_READ);
91 ns = accept(s, &sin, &len);
97 fprintf(stderr, "no more connections");
100 printf("got connection from %s\n", inet_ntoa(sin.sin_addr));
101 ctx = (struct context *)malloc(sizeof(*ctx));
104 ctx->us = lwpCreate(SP_READER, readConn, 8192, 0, 0, ctx);
115 lwpCreate(SP_ACCEPT, acceptConn, 8192, argc, argv, 0);