- Code: Select all
//----------socket handling
int sockfd, numbytes;
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
he=gethostbyname(host);
if (he== NULL) { // get the host info
//cout<<"Error resolving host"<<endl;
return -3;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
//cout<<"Error creating socket"<<endl;
return -2;
}
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
//cout<<endl<<endl<<"connecting"<<endl;
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
//cout<<"Error connecting"<<endl;
return -1;
}
close(sockfd);
//-----------end socket handling
I've heard of setsockopt, I just can't figure out how to properly use it, none of the tutorials I find show any good examples.
