main() { struct sockaddr_in sa; int sa_len; char hostname[1024]; sa.sin_port = htons(7777); gethostname(hostname, 1024); struct hostent *hp = gethostbyname(localhostname); bcopy((char *)hp->haddr, (char *)&sa.sin_addr, hp->h_length); int sock = socket(hp->h_addrtype, SOCK_STREAM, 0); bind(s, (struct sockaddr *)&sa, sizeof(sa)); listen(s, 99); int fd = accept(s, (struct sockaddr *)&sa, (unsigned int *)sa_len); sleep(3600); // wait for one hour write(fd, "Hi\0", 3); } |
main() { struct sockaddr_in sa; int sa_len; sa.sin_port = htons(7777); struct hostent *hp = gethostbyname("euclid.nmu.edu"); bcopy((char *)hp->haddr, (char *)&sa.sin_addr, hp->h_length); int sock = socket(hp->h_addrtype, SOCK_STREAM, 0); int fd = connect(sock, (struct sockaddr *)sa, sizeof(sa)); read(fd, buf, 1024); cout << buf << endl; } |