/////////////////////////////////////////////////////////////////////////////////////// // BiffSocko // biffclient.cpp // // PLEASE SEE THE README // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. /////////////////////////////////////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h>#define PORT 3050 #define MAXDATASIZE 1024 ///////////////////////////////// // sockclient class ///////////////////////////////// class sockclient{ public: int opensock(char*); }; ///////////////////////////////// // create a socket and connect to // the server ///////////////////////////////// int sockclient::opensock(char *host){ int sockfd; struct hostent *he; struct sockaddr_in their_addr; // connector's address information if ((he=gethostbyname(host)) == NULL) { // get the host info perror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } return(sockfd); } ///////////////////////////// // driver program ///////////////////////////// int main(int argc, char *argv[]) { sockclient sock1; int descriptor; char buf[MAXDATASIZE]; FILE *sockin, *sockout; if (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } descriptor = sock1.opensock(argv[1]); if(!(sockin = fdopen(descriptor, "r"))){ perror("cant open sockin file\n"); exit(1); } if(!(sockout = fdopen(descriptor, "w"))){ perror("cant open sockout file\n"); exit(1); } while(fgets(buf,MAXDATASIZE-1,sockin)){ printf("%s\n",buf); memset(buf,0,0); } fclose(sockin); fclose(sockout); close(descriptor); return 0; }