/* a C++ socket client class Keith Vertanen 11/98 */ #define BACKLOG 10 // how many pending connections queue will hold #define VERBOSE 1 // turn on or off debugging output #include "client.h" Client::Client(int p, int datap, char *myhost, int rev) { port = p; dataport = datap; if (VERBOSE) printf("Server: opening socket to %s on port = %d, datagrams on port = %d\n", myhost, port, dataport); if ((he=gethostbyname(myhost))==NULL) perror("gethostbyname"); if ((new_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) perror("socket"); if ((datasockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("datagram socket"); exit(1); } if ((senddatasockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("datagram send socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(dataport); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero), 8); their_addr.sin_family = AF_INET; their_addr.sin_port = htons(port); their_addr.sin_addr = *((struct in_addr *) he->h_addr); bzero(&(their_addr.sin_zero), 8); if (connect(new_fd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect new_fd"); exit(1); } if (bind(datasockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind datagram socket"); exit(1); } // send out request for reversed bits or not char temp[1]; if (rev) { temp[0] = 1; send(new_fd, temp, 1, 0); } else { temp[0] = 0; send(new_fd, temp, 1, 0); } }; // send a string to the socket void Client::send_string(char *str) { if (send(new_fd, (char *) str, strlen(str), 0) == -1) perror("send"); if (VERBOSE) printf("Server: sending string '%s'\n", str); recv_ack(); send_ack(); }; // send some integers over the wire void Client::send_ints(int *val, int len) { int *buff; char *ptr, *valptr; int i,j; // we may need to reverse the byte order, oh joy if (REVERSE) { // set the buff pointer to our previously allocated spot buff = (int *) buffer; ptr = (char *) buff; valptr = (char *) val; // we need to reverse the order of each set of bytes for (i = 0; i < len; i++) { for (j=0; j