Hey,
my program requires a working SMTP server. So i do a first check on startup.
This is the test:
Code:
bool Mail::checkSMTP(){
char echoBuffer[64];
int bytesRcvd = 0;
int create_socket;
struct sockaddr_in address;
char* answerCode;
if((create_socket = socket(AF_INET, SOCK_STREAM,0)) > 0){
// Socket created.
address.sin_family = AF_INET;
address.sin_port = htons(smtpport);
address.sin_addr.s_addr = resolveName(hostname);
if(connect(create_socket,(struct sockaddr *) &address, sizeof(address)) == 0){
// Connected to server. - Get answer.
if((bytesRcvd = recv(create_socket, echoBuffer, 63,0)) <= 0) return 0;
answerCode = strtok(echoBuffer, " ");
if(strcmp(answerCode,"220") == 0){
close(create_socket);
return 1;
}else{
return 0;
}
}else{
// Could not connect to server.
close(create_socket);
return 0;
}
}
return 0;
}
The Problem: There is no timeout! When there is a firewall or a HTTP service behind the port the startup just stops because i get no answer.
What would be the best way to solve this problem?
Thank you!