int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); ipaddr addr = iplocal(NULL, port, 0); tcpsock ls = tcplisten(addr, 10); if(!ls) { perror("Can't open listening socket"); return 1; } while(1) { tcpsock as = tcpaccept(ls, -1); printf("New connection!\n"); tcpsend(as, "What's your name?\r\n", 19, -1); tcpflush(as, -1); tcpclose(as); } return 0; }
int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); tcpsock ls = tcplisten(iplocal(NULL, port, 0)); if(!ls) { perror("Can't open listening socket"); return 1; } while(1) { tcpsock as = tcpaccept(ls, -1); tcpsend(as, "What's your name?\r\n", 19, -1); tcpflush(as, -1); char inbuf[256]; size_t sz = tcprecvuntil(as, inbuf, sizeof(inbuf), "\r", 1, -1); inbuf[sz - 1] = 0; char outbuf[256]; int rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!\n", inbuf); sz = tcpsend(as, outbuf, rc, -1); tcpflush(as, -1); tcpclose(as); } }
static coroutine void connector(void) { tcpsock s = tcpconnect(iplocal("127.0.0.1", 5555, 0), -1); assert(s); char buf[1]; tcprecv(s, buf, 1, -1); assert(0); }
int main(int argc, char *argv[]) { int port = 5555; if (argc > 1) { port = atoi(argv[1]); } // create a communication channel chan channel = chmake(int, 0); go(channel_handler(channel)); // create server that listens and routes traffic to handler function ipaddr addr = iplocal(NULL, port, 0); tcpsock ls = tcplisten(addr); if (!ls) { perror("Can't open listening socket"); return 1; } while(1) { tcpsock accepted_socket = tcpaccept(ls, -1); if (!accepted_socket) { continue; } else { go(handler(accepted_socket, channel)); } // temporary stop gap :) if (test == 3) { break; } } return 0; }
int main(int argc, char *argv[]) { if(argc != 3) { printf("usage: c10k <parallel-connections> <roundtrip-count>\n"); return 1; } long conns = atol(argv[1]); long roundtrips = atol(argv[2]); assert(conns >= 1); tcpsock ls = tcplisten(iplocal("127.0.0.1", 5555, 0), 10); assert(ls); int i; for(i = 0; i != conns - 1; ++i) { go(connector()); tcpsock as = tcpaccept(ls, -1); assert(as); } go(sender(roundtrips)); tcpsock as = tcpaccept(ls, -1); assert(as); int64_t start = now(); char buf[1]; size_t nbytes; for(i = 0; i != roundtrips; ++i) { nbytes = tcpsend(as, "A", 1, -1); assert(errno == 0); assert(nbytes == 1); tcpflush(as, -1); assert(errno == 0); nbytes = tcprecv(as, buf, 1, -1); assert(errno == 0); assert(nbytes == 1); assert(buf[0] == 'A'); } int64_t stop = now(); long duration = (long)(stop - start); long us = (duration * 1000) / roundtrips; printf("done %ld roundtrips in %f seconds\n", roundtrips, ((float)duration) / 1000); printf("duration of a single roundtrip: %ld us\n", us); printf("roundtrips per second: %ld\n", (long)(roundtrips * 1000 / duration)); return 0; }
int main() { udpsock s1 = udplisten(iplocal(NULL, 5555, 0)); udpsock s2 = udplisten(iplocal(NULL, 5556, 0)); ipaddr addr = ipremote("127.0.0.1", 5556, 0, -1); while(1) { udpsend(s1, addr, "ABC", 3); assert(errno == 0); char buf[3]; size_t sz = udprecv(s2, &addr, buf, sizeof(buf), now() + 100); if(errno == ETIMEDOUT) continue; assert(errno == 0); assert(sz == 3); break; } while(1) { udpsend(s2, addr, "DEF", 3); assert(errno == 0); char buf[3]; size_t sz = udprecv(s1, &addr, buf, sizeof(buf), now() + 100); if(errno == ETIMEDOUT) continue; assert(errno == 0); assert(sz == 3); break; } udpclose(s2); udpclose(s1); return 0; }
static coroutine void sender(long roundtrips) { tcpsock s = tcpconnect(iplocal("127.0.0.1", 5555, 0), -1); assert(s); char buf[1]; int i; size_t nbytes; for(i = 0; i != roundtrips; ++i) { nbytes = tcprecv(s, buf, 1, -1); assert(errno == 0); assert(nbytes == 1); assert(buf[0] == 'A'); nbytes = tcpsend(s, "A", 1, -1); assert(errno == 0); assert(nbytes == 1); tcpflush(s, -1); assert(errno == 0); } }
int main() { char buf[16]; tcpsock ls = tcplisten(iplocal(NULL, 5555, 0), 10); assert(ls); int fd = tcpdetach(ls); assert(fd != -1); ls = tcpattach(fd, 1); assert(ls); assert(tcpport(ls) == 5555); go(client(5555)); tcpsock as = tcpaccept(ls, -1); /* Test deadline. */ int64_t deadline = now() + 30; size_t sz = tcprecv(as, buf, sizeof(buf), deadline); assert(sz == 0 && errno == ETIMEDOUT); int64_t diff = now() - deadline; assert(diff > -20 && diff < 20); sz = tcpsend(as, "ABC", 3, -1); assert(sz == 3 && errno == 0); tcpflush(as, -1); assert(errno == 0); sz = tcprecvuntil(as, buf, sizeof(buf), "\n", 1, -1); assert(sz == 4); assert(buf[0] == '1' && buf[1] == '2' && buf[2] == '3' && buf[3] == '\n'); sz = tcprecvuntil(as, buf, sizeof(buf), "\n", 1, -1); assert(sz == 3); assert(buf[0] == '4' && buf[1] == '5' && buf[2] == '\n'); sz = tcprecvuntil(as, buf, 3, "\n", 1, -1); assert(sz == 3); assert(buf[0] == '6' && buf[1] == '7' && buf[2] == '8'); tcpclose(as); tcpclose(ls); return 0; }
int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); ipaddr addr = iplocal(NULL, port, 0); tcpsock ls = tcplisten(addr, 10); if(!ls) { perror("Can't open listening socket"); return 1; } while(1) { tcpsock as = tcpaccept(ls, -1); if(!as) continue; go(dialogue(as)); } }
int main(int argc, char *argv[]) { int port = 5555; int nproc = 1; if(argc > 1) port = atoi(argv[1]); if(argc > 2) nproc = atoi(argv[2]); ipaddr addr = iplocal(NULL, port, 0); tcpsock ls = tcplisten(addr, 10); if(!ls) { perror("Can't open listening socket"); return 1; } int i; for (i = 0; i < nproc - 1; ++i) { pid_t pid = fork(); if(pid < 0) { perror("Can't create new process"); return 1; } if(pid > 0) break; } chan ch = chmake(int, 0); go(statistics(ch)); while(1) { tcpsock as = tcpaccept(ls, -1); if(!as) continue; go(dialogue(as, ch)); } }
int main() { char buf[16]; SSLSERVER_p_st ss = malloc(sizeof(SSLSERVER_st)); ss->method = NULL; ss->addr = iplocal(NULL, 5555, 0); ss->cert_file = "./tests/cert.pem"; ss->key_file = "./tests/key.pem"; sslsock ls = ssllisten(ss, 10); assert(ls); go(client(5555)); sslsock as = sslaccept(ls, -1); /* Test port. */ assert(sslport(as) != 5555); /* Test deadline. */ int64_t deadline = now() + 30; size_t sz = sslrecv(as, buf, sizeof(buf), deadline); assert(sz == 0 && errno == ETIMEDOUT); int64_t diff = now() - deadline; assert(diff > -20 && diff < 20); sz = sslsend(as, "ABC", 3, -1); assert(sz == 3 && errno == 0); sslflush(as, -1); assert(errno == 0); /* Test sslrecvuntil. */ sz = sslrecvuntil(as, buf, sizeof(buf), "\n", 1, -1); assert(sz == 4); assert(buf[0] == '1' && buf[1] == '2' && buf[2] == '3' && buf[3] == '\n'); sz = sslrecvuntil(as, buf, sizeof(buf), "\n", 1, -1); assert(sz == 3); assert(buf[0] == '4' && buf[1] == '5' && buf[2] == '\n'); sz = sslrecvuntil(as, buf, 3, "\n", 1, -1); assert(sz == 3); assert(buf[0] == '6' && buf[1] == '7' && buf[2] == '8'); sslclose(as); sslclose(ls); /* Test whether libmill performs correctly when faced with TCP pushback. */ ss->method = NULL; ss->addr = iplocal(NULL, 5555, 0); ss->cert_file ="./tests/cert.pem"; ss->key_file ="./tests/key.pem"; ls = ssllisten(ss, 10); go(client2(5555)); as = sslaccept(ls, -1); assert(as); char buffer[2048]; while(1) { size_t sz = sslsend(as, buffer, 2048, -1); if(errno == ECONNRESET) break; if(errno != 0) { fprintf(stderr, "errno=%d\n", errno); assert(0); } sslflush(as, -1); if(errno == ECONNRESET) break; if(errno != 0) { fprintf(stderr, "errno=%d\n", errno); assert(0); } assert(errno == 0); } sslclose(as); sslclose(ls); free(ss); return 0; }
int main() { ipaddr addr = iplocal("127.0.0.1", 5555, 0); /* No subprotocols used. */ wsock ls = wsocklisten(addr, NULL, 10); assert(ls); go(client(NULL, NULL)); wsock s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), NULL)); wsockclose(s); wsockclose(ls); /* Subprotocol used by client but not by server. */ ls = wsocklisten(addr, NULL, 10); assert(ls); go(client("sp1", "sp1")); s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), "sp1")); wsockclose(s); wsockclose(ls); /* Subprotocol used by server but not by client. */ ls = wsocklisten(addr, "sp1", 10); assert(ls); go(client(NULL, NULL)); s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), "sp1")); wsockclose(s); wsockclose(ls); /* Same subprotocol used by both server and client. */ ls = wsocklisten(addr, "sp1", 10); assert(ls); go(client("sp1", "sp1")); s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), "sp1")); wsockclose(s); wsockclose(ls); /* Client selecting one of the server's subprotocols. */ ls = wsocklisten(addr, "sp1,sp2,sp3", 10); assert(ls); go(client("sp2", "sp2")); s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), "sp2")); wsockclose(s); wsockclose(ls); /* Client asking for subprotocol not supported by server. */ ls = wsocklisten(addr, "sp1,sp2,sp3", 10); assert(ls); go(badclient("sp4", ECONNRESET)); wsockclose(ls); /* Client selecting two of the server's subprotocols. First one should be preferred by the server. */ ls = wsocklisten(addr, "sp1,sp2,sp3", 10); assert(ls); go(client("sp2,sp1", "sp2")); s = wsockaccept(ls, -1); assert(s); assert(wsock_str_eq(wsocksubprotocol(s), "sp2")); wsockclose(s); wsockclose(ls); return 0; }
void daemon(void) { tcpmuxd(iplocal(NULL, 5557, 0)); assert(0); }