#include #include #include #include #include #include #include #include int main(int argc, char * argv[]) { int sockfd; struct sockaddr_in addr; char buf[100]; if (argc < 3) { printf("Usage: a.out ip_addr port.\n"); exit(0); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror(": Can't get socket"); exit(1); } // preparing server socket address memset(&addr, 0, sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; addr.sin_port = htons((short)atoi(argv[2])); addr.sin_addr.s_addr = inet_addr(argv[1]); fprintf(stderr, "here 1\n"); // connecting to the server if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror(": connect"); exit(1); } // connection established fprintf(stderr, "here 2\n"); printf("Connected.\n"); fflush(0); // send an integer (directly) { int ii; ii = 100; printf("ii= %d %x\n", ii, ii); write(sockfd, &ii , sizeof(int)); } // Then we read from standard input and write to socket while (fgets(buf, 100, stdin)) { write(sockfd, buf, strlen(buf)+1); if (read(sockfd, buf, 100)==0) { printf("server closed the connection.\n"); exit(0); }; // buf must be null terminated string // server does not send back the integer value printf("Server response : %s\n", buf); } }