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); } }
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() { tcpsock ls = tcplisten("*:5555"); assert(ls); go(client()); tcpsock as = tcpaccept(ls); tcpsend(as, "ABC", 3); int rc = tcpflush(as); assert(rc == 0); char buf[16]; ssize_t sz = tcprecvuntil(as, buf, sizeof(buf), '\n'); assert(sz == 4); assert(buf[0] == '1' && buf[1] == '2' && buf[2] == '3' && buf[3] == '\n'); sz = tcprecvuntil(as, buf, sizeof(buf), '\n'); assert(sz == 3); assert(buf[0] == '4' && buf[1] == '5' && buf[2] == '\n'); sz = tcprecvuntil(as, buf, 3, '\n'); assert(sz == 0); 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]); } // 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; }
wsock wsocklisten(ipaddr addr, const char *subprotocol, int backlog) { /* Check the arguments. */ if(!wsock_checkstring(subprotocol)) return NULL; struct wsock *s = (struct wsock*)malloc(sizeof(struct wsock)); if(!s) {errno = ENOMEM; return NULL;} s->flags = WSOCK_LISTENING; s->u = tcplisten(addr, backlog); if(!s->u) {free(s); return NULL;} wsock_str_init(&s->url, NULL, 0); wsock_str_init(&s->subprotocol, subprotocol, wsock_str_len(subprotocol)); return s; }
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() { 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)); } }
void server(const char *ip){ int sock; sock = tcplisten(SPORT); if(sock < 0){ perror("server"); exit(1); } printf("wait...\n"); while(1){ int cs; struct sockaddr_storage sa; socklen_t len = sizeof(sa); cs = accept(sock, (struct sockaddr *)&sa, &len); if(cs<0){ perror("accept"); exit(1); } printf("serv - accept.\n"); int read_size; char readbuf[BUFLEN]; read_size = read_line(cs, readbuf); if(read_size == 0)break; printf("serv - read: %s\n", readbuf); char writebuf[BUFLEN]; int clires; clires = client(ip, writebuf); if(clires < 0){ sprintf(writebuf, "::1\n"); } write(cs, writebuf, strlen(writebuf)); printf("serv - write: %s\n", writebuf); printf("serv - reject.\n"); //close(cs); } }
void server(char *ip6){ int sock; sock = tcplisten(PORT); if(sock < 0){ perror("server"); exit(1); } printf("PORT: %s, wait...\n", PORT); while(1){ /* int cs; struct sockaddr_storage sa; socklen_t len = sizeof(sa); cs = accept(sock, (struct sockaddr *)&sa, &len); if(cs<0){ perror("accept"); exit(1); } printf("accept.\n"); */ int read_size; char buf[BUFLEN]; //read_size = read_line(cs, buf); //if(read_size == 0)break; struct sockaddr *src; socklen_t *len; recvfrom(sock, buf, sizeof(buf), 0, src, len); printf("mes: %s", buf); char writebuf[BUFLEN]; sprintf(writebuf, "%s\n", ip6); //write(cs, writebuf, strlen(writebuf)); sendto(sock, writebuf, sizeof(writebuf), 0, src, *len); printf("reject.\n"); //close(cs); } }
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)); } }
BCPLWORD callc(BCPLWORD *args, BCPLWORD *g) { int rc = 0; BCPLWORD fno = args[0]; //printf("\nCallc: fno = %d\n", fno); switch(fno) { default: return -1; case c_name2ipaddr: // name => ipaddr (host format) b2c_str(args[1], namebuf); //printf("Callc c_name2ipaddr: args[1]=%d %s\n", // args[1], namebuf); return name2ipaddr(namebuf); case c_name2port: // name => port (host format) b2c_str(args[1], namebuf); //printf("callc c_name2port: %s\n", namebuf); return name2port(namebuf); case c_newsocket: // Allocate a new socket return newsocket(); case c_reuseaddr: // Reuse address return reuseaddr((int)args[1], (int)args[2]); case c_setsndbufsz: // Set the send buffer size return setsndbufsz((int)args[1], (int)args[2]); case c_setrcvbufsz: // Set the recv buffer size return setrcvbufsz((int)args[1], (int)args[2]); case c_tcpbind: // Bind a socket to a given ipaddr/port //printf("c_tcpbind: %d %08x %d\n", args[1], args[2], args[3]); return tcpbind((int)args[1], (int)args[2], (int)args[3]); case c_tcpconnect: // Connect a socket to a given ipaddr/port //printf("tcpconnect %d %08x %d\n", args[1], args[2], args[3]); return tcpconnect((int)args[1], (int)args[2], (int)args[3]); case c_tcplisten: // Cause a socket to listen return tcplisten((int)args[1], (int)args[2]); case c_tcpaccept: // Cause a socket to accept a connection return tcpaccept((int)args[1]); case c_tcpclose: // Close a connection //printf("tcpclose %d\n", args[1]); return close((int)args[1]); case c_fd_zero: // Clear all bits in an fd_set { //fd_set *bits = (fd_set*)&W[args[2]]; FD_ZERO((fd_set*)&W[args[1]]); return 0; } case c_fd_set: // Set a bit in an fd_set //printf("c_fd_set: args[1]=%d args[2]=%d\n", args[1], args[2]); FD_SET((int)args[1], (fd_set*)&W[args[2]]); return 0; case c_fd_isset: // Test a bit in an fd_set return FD_ISSET((int)args[1], (fd_set*)&W[args[2]]); case c_select: // Call the select function { int i, rc; int s = (int) args[1]; fd_set *rd_set = (fd_set *) &W[args[2]]; fd_set *wr_set = (fd_set *) &W[args[3]]; fd_set *er_set = (fd_set *) &W[args[4]]; struct timeval *timeval = (struct timeval *) ((args[5]==0) ? NULL : &W[args[5]]); /* //for(i=0; i<10;i++) // printf("callc: rdset bit %d = %d\n", i, FD_ISSET(i, (fd_set*)&W[args[2]])); */ //printf("callc: calling select(%d,%d,%d,%d,%d)\n", // args[1],args[2],args[3],args[4],args[5]); //if(timeval) { //printf("c_select: tv_sec = %d\n", timeval->tv_sec); //printf("c_select: tv_usec = %d\n", timeval->tv_usec); //} rc = select(s, rd_set, wr_set, er_set, timeval); if(rc==-1) perror("select returned error"); //printf("\ncallc: select => rc = %d\n", rc); //for(i=0; i<10;i++) // printf("callc: rdset bit %d = %d\n", // i, FD_ISSET(i, rd_set)); return rc; } case c_recv: // Call the recv(s, buf, len, flags) { int s = (int)args[1]; char *buf = (char*)&W[args[2]]; int len = (int)args[3]; int flags = (int)args[4]; int rc = 0; //printf("cfuncs: Calling recv(%d, %d, %d, %d)\n", // args[1], args[2], args[3], args[4]); rc = recv(s, buf, len, flags); if(rc==-1)perror("recv returned error"); //printf("cfuncs: recv returned rc=%d\n", rc); return rc; } case c_send: // Call the send(s, buf, len, flags) { int s = (int)args[1]; char *buf = (char*)&W[args[2]]; int len = (int)args[3]; int flags = (int)args[4]; int rc = 0; //printf("cfuncs: Calling send(%d, %d, %d, %d)\n", // args[1], args[2], args[3], args[4]); rc = send(s, buf, len, flags); if(rc==-1)perror("send returned error"); //printf("cfuncs: send returned rc=%d\n", rc); return rc; } case c_read: // Call the read(s, buf, len) { int s = (int)args[1]; char *buf = (char*)&W[args[2]]; int len = (int)args[3]; int rc = 0; //printf("cfuncs: Calling read(%d, %d, %d)\n", args[1], args[2], args[3]); rc = read(s, buf, len); //if(rc==-1)perror("read returned error"); //printf("cfuncs: read returned rc=%d\n", rc); return rc; } case c_write: // Call the write(s, buf, len) { int s = (int) args[1]; char *buf = (char*) &W[args[2]]; int len = (int) args[3]; int rc = 0; //printf("cfuncs: Calling write(%d, %d, %d)\n", args[1], args[2], args[3]); rc = write(s, buf, len); if(rc==-1)perror("read returned error"); //printf("cfuncs: read returned rc=%d\n", rc); return rc; } } }