00001 #include "connect.h"
00002 int connect(hostent * he, int port) {
00003 int sockfd;
00004 struct sockaddr_in their_addr;
00005 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00006 cerr << "Could not connect to " << he->h_name;
00007 return -1;
00008 }
00009
00010 their_addr.sin_family = AF_INET;
00011 their_addr.sin_port = htons(port);
00012 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
00013 memset(&(their_addr.sin_zero), 0,8);
00014 int sd = connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr));
00015 if (sd == -1) {
00016 cerr << "Could not connect to " << he->h_name;
00017 return -1;
00018 }
00019 return sockfd;
00020 }