/* tcp client skeleton */ #include #include #include #include #include #include #include #include #include /* argv[0] is always the name of the program itself (implicit) argv[1] will be ip address of server argv[2] will be port number of server */ int main(int argc, char **argv) { int cfd; // communication file descriptor (handle on socket) // declare structure to hold socket address info: struct sockaddr_in serveraddr; int retval; // many functions return values to indicate success/failure int x; // some data unsigned char buffer[128]; // binary data buffer (sample) serveraddr.sin_family = AF_INET; // always for IPv4 serveraddr.sin_addr.s_addr = inet_addr(argv[1]); serveraddr.sin_port = htons(atoi(argv[2])); cfd = socket(AF_INET,SOCK_STREAM,0); // register tcp socket with OS // make connection to server. retval = connect(cfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); // note the type cast to more generic type (polymorphism) if (retval != 0) { perror("socket failure!"); exit(1); } // at this point, tcp connection is in ESTABLISHED state, and // communication with peer can begin: (yeah!) read(cfd,&x,sizeof(int)); // read 4 bytes x = ntohl(x); // convert to host byte ordering read(cfd,buffer,x); // read x bytes into buffer // note: buffer already pointer, no & write(cfd,buffer+120,8); // write last 8 bytes of buffer to socket printf("complete\n"); close(cfd); // close socket exit(0); } /* Compile on Redhat Linux with "gcc clientskl.c" Solaris and other Unix systems (possibly cygwin also) may require "gcc -lnsl -lsocket clientskl.c" */