// internet_client.cpp #include "includes.h" #include "internet_client.h" internet_client::internet_client(char *address, int port) { strcpy(server_name,address); port_number=port; allOK=0; struct hostent* server_info_ptr=gethostbyname(server_name); if (server_info_ptr==NULL) { fprintf(stderr,"host \"%s\" not known\n", server_name); return; } struct in_addr* coded_server_addr=(struct in_addr*)server_info_ptr->h_addr; if ((coded_server_addr->s_addr)==NULL) { fprintf(stderr,"host %s not known\n", inet_ntoa(*coded_server_addr)); return; } struct sockaddr_in server_internet_addr; struct sockaddr *sia_ptr=(struct sockaddr *)&server_internet_addr; int sia_len=sizeof(server_internet_addr); bzero(sia_ptr,sia_len); server_internet_addr.sin_family=AF_INET; server_internet_addr.sin_addr.s_addr=coded_server_addr->s_addr; server_internet_addr.sin_port=htons(port_number); connection_socket=socket(AF_INET,SOCK_STREAM,0); if (connection_socket<0) { fprintf(stderr,"could not create an internet socket\n"); perror(""); return; } printf("\n[Connecting to %s (%s) on port %d]\n", server_name,inet_ntoa(*coded_server_addr),port_number); int n=connect(connection_socket,sia_ptr,sia_len); if (n<0) { fprintf(stderr,"Could not make connection to %s:%d\n",server_name,port_number); perror(""); return; } read_file=fdopen(connection_socket,"r"); write_file=fdopen(connection_socket,"w"); if (read_file==NULL || write_file==NULL) { fprintf(stderr,"made a bad connection\n"); return; } allOK=1; printf("connected OK\n"); } void internet_client::disconnect(void) { if (!allOK) return; allOK=false; close(connection_socket); }