#include #include #include #include #include #include #include #include main (int argc, char * argv[]) { int s,rlen; struct hostent *server_info; struct sockaddr_in where; struct protoent * myproto; char buf[200]; char * protoName = "tcp"; char * hostname = "cslab100.cs.edinboro.edu"; struct in_addr tmp; myproto = getprotobyname(protoName); if (NULL == myproto) { sprintf(buf, "Could not get protocol %s",protoName); perror(buf); exit(-1); } if ((s = socket(AF_INET, SOCK_STREAM, myproto->p_proto)) == -1){ perror("Socket not created "); exit(-1); } if (NULL == (server_info = gethostbyname(hostname))){ sprintf(buf, "Name lookup failed for %s",hostname); perror(buf); exit(-1); } printf("\tConnecting to host %s\n", server_info->h_name); if (server_info ->h_addrtype == AF_INET) { printf("\tThis is a IPV4 address\n"); } if (server_info ->h_addrtype == AF_INET6) { printf("\tThis is a IPV6 address\n"); } printf("\tThe address is %d bytes long.\n", server_info->h_length); memcpy(&tmp,server_info->h_addr_list[0], server_info->h_length); printf("\tWhich is %s\n",inet_ntoa(tmp)); where.sin_addr.s_addr = *((u_long *) server_info->h_addr_list[0]); where.sin_port = htons(5600); where.sin_family = AF_INET; if (-1 == connect(s, (struct sockaddr *) &where, sizeof(struct sockaddr_in))){ perror("Connection failed"); exit(-1); } printf("connected to the server, sending a message\n"); strcpy(buf,"Hello there how are you Mr. Server?\n"); write(s,buf,strlen(buf)); buf[0] = 0; rlen = read(s,buf,200); buf[rlen] = '\0'; printf("The boss said:%s\n",buf); }