// set family and socket type
struct addrinfo hints, *result;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// resolve hostname to IP address: specify host and port number (in char not int)
if(getaddrinfo("smtp.gmail.com", "25", &hints, &result) != 0)
{
freeaddrinfo(result);
puts("Could not resolve hostname.");
exit(1);
}
// create socket and free addrinfo memory
int newsocket = socket(result->ai_family, result->ai_socktype, 0);
if(newsocket == -1)
{
puts("Could not create socket.");
freeaddrinfo(result); // free addrinfo memory
close(newsocket);
exit(1);
}
// set socket timeouts
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout)); // zero timeout struct before use
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(newsocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); // send timeout
setsockopt(newsocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // receive timeout