int main(int argc, char *argv[]) { if(argc != 4) { fprintf(stderr, "Usage: wget [protocol] [server] [resource]\n"); return 1; } int port; if(strcmp(argv[1], "http") == 0) port = 80; else if(strcmp(argv[1], "https") == 0) port = 443; else { fprintf(stderr, "Unsupported protocol.\n"); return 1; } struct ipaddr addr; int rc = ipaddr_remote(&addr, argv[2], port, 0, -1); if(rc != 0) { perror("Cannot resolve server address"); return 1; } int s = tcp_connect(&addr, -1); if(s < 0) { perror("Cannot connect to the remote server"); return 1; } rc = tcp_close(s, -1); assert(rc == 0); return 0; }
coroutine void client(void) { ipaddr addr; int rc = ipaddr_remote(&addr, "127.0.0.1", 5555, 0, -1); assert(rc == 0); int s = tcp_connect(&addr, -1); assert(s >= 0); int cs = crlf_start(s); assert(cs >= 0); rc = msend(cs, "ABC", 3, -1); assert(rc == 0); char buf[3]; ssize_t sz = mrecv(cs, buf, 3, -1); assert(sz == 3); assert(buf[0] == 'G' && buf[1] == 'H' && buf[2] == 'I'); rc = msend(cs, "DEF", 3, -1); assert(rc == 0); s = crlf_stop(cs, -1); assert(s >= 0); rc = hclose(s); assert(rc == 0); }