#include #include #include #include #include #include #include #include main() { int i, fd, fd2, len; struct hostent *hp; struct protoent *pp; char hostname[64]; struct sockaddr_in lAddr, rAddr; char* data; int datalen, datapos; gethostname(hostname, 64); pp = getprotobyname("tcp"); hp = gethostbyname(hostname); assert(pp && hp); fd = socket(AF_INET, SOCK_STREAM, pp->p_proto); assert(fd >= 0); lAddr.sin_family = hp->h_addrtype; memcpy((char *)&lAddr.sin_addr.s_addr, (char *)hp->h_addr, sizeof(lAddr.sin_addr.s_addr)); lAddr.sin_port = htons(12345); i = bind(fd, (struct sockaddr *)&lAddr, sizeof(lAddr)); assert(i >= 0); printf("listening to port %d host %s (%s)\n", ntohs(lAddr.sin_port), hostname, inet_ntoa(lAddr.sin_addr)); i = listen(fd, 5); assert(i >= 0); len = sizeof(rAddr); memset(&rAddr, 0, sizeof(rAddr)); fd2 = accept(fd, (struct sockaddr *)&rAddr, &len); assert(fd2 >= 0); printf("got connection from %s\n", inet_ntoa(rAddr.sin_addr)); i = fcntl(fd2, F_SETFL, O_NDELAY); assert(i >= 0); datalen = (int) 1e8; data = (char *) malloc(datalen); datapos = 0; while (datapos < datalen) { fd_set wfds; FD_ZERO(&wfds); FD_SET(fd2, &wfds); i = select(fd2 + 1, NULL, &wfds, NULL, NULL); assert(i == 1); printf("trying to write %d bytes\n", datalen - datapos); i = write(fd2, data + datapos, datalen - datapos); printf("%d bytes written\n", i); assert(i > 0); datapos += i; assert(datapos <= datalen); } printf("hit return to exit "); getchar(); exit(0); }