/* * simple_client_simple.c * a stream socket client demo * * Created by Angelos Stavrou on 9/16/08. * Copyright 2008 George Mason University. All rights reserved. * */ #include #include #include #include #include #include #include #include #include #define PORT 15000 // the port client will be connecting to #define MAXDATASIZE 1400 // max number of bytes we can get at once int main(int argc, char *argv[]) { int sockfd, numbytes; // socket descriptor and number of bytes. char buf[MAXDATASIZE]; struct hostent *remotehost; struct sockaddr_in remote_addr; // connector's address information if (argc != 3) { fprintf(stderr,"usage: socket_client_simple hostname message\n"); exit(1); } if ((remotehost=gethostbyname(argv[1])) == NULL) { // get the host info from the command line perror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } remote_addr.sin_family = AF_INET; // host byte order remote_addr.sin_port = htons(PORT); // short, network byte order remote_addr.sin_addr = *((struct in_addr *)remotehost->h_addr); memset(remote_addr.sin_zero, '\0', sizeof remote_addr.sin_zero); if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof remote_addr) == -1) { perror("connect"); exit(1); } if (send(sockfd, argv[2], sizeof(argv[2]), 0) == -1) perror("send"); if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; printf("Received: %s",buf); close(sockfd); return 0; }