#include #include #include #include #include #include #define PORTNUMBER 12345 int main(void) { int n, s, len; char buf[1024]; char hostname[64]; struct hostent *hp; struct sockaddr_in name; /* * Get our local host name. */ if (gethostname(hostname, sizeof(hostname)) < 0) { perror("gethostname"); exit(1); } /* * Look up our host's network address. */ if ((hp = gethostbyname(hostname)) == NULL) { fprintf(stderr, "unknown host: %s.\n", hostname); exit(1); } /* * Create a socket in the INET * domain. */ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } /* * Create the address of the server. */ memset(&name, 0, sizeof(struct sockaddr_in)); name.sin_family = AF_INET; name.sin_port = htons(PORTNUMBER); memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length); len = sizeof(struct sockaddr_in); /* * Connect to the server. */ if (connect(s, (struct sockaddr *) &name, len) < 0) { perror("connect"); exit(1); } /* * Read from standard input, and copy the * data to the socket. */ while ((n = read(0, buf, sizeof(buf))) > 0) { if (send(s, buf, n, 0) < 0) { perror("send"); exit(1); } } close(s); exit(0); }