Check SMTP server on startup

This is a discussion on Check SMTP server on startup within the C++ Programming forums, part of the General Programming Boards category; Hey, my program requires a working SMTP server. So i do a first check on startup. This is the test: ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13

    Check SMTP server on startup

    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!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Use ethereal to observe what a real email client does in that circumstance, and compare the traffic with that produced by your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    Quote Originally Posted by Salem View Post
    Use ethereal to observe what a real email client does in that circumstance, and compare the traffic with that produced by your program.
    alpine does a DNS request and then starts to communicate with the SMTP server like i do.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    So if you're producing the same message sequence, why doesn't it work?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    Quote Originally Posted by Salem View Post
    So if you're producing the same message sequence, why doesn't it work?
    I dont know.

    I just configured alpine to connect to my SMTP server on port 80. It uses a timeout. After about 1 minute i get "Connection timed out"

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    AFAIK, 80 is for HTTP. And SMTP uses 25, according to wikipedia.

    And make sure your firewall isn't blocking it.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13

    Smile

    Quote Originally Posted by robwhit View Post
    AFAIK, 80 is for HTTP. And SMTP uses 25, according to wikipedia.

    And make sure your firewall isn't blocking it.
    Well... Ehm... Yes. Read my first post again

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    I would suggest you compare the message sequences again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by crisis View Post
    Well... Ehm... Yes. Read my first post again
    Maybe the firewall is blocking the program on a per-program basis? IDK which end you are having the firewall on, the client side or the server side.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    Quote Originally Posted by robwhit View Post
    Maybe the firewall is blocking the program on a per-program basis? IDK which end you are having the firewall on, the client side or the server side.
    My problem is not that the connection is blocked. I just don't want the program to stop when a connection is blocked. This usually does not happen - But what if it does?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Oh I see.

    You could use threads or asynchronous sockets.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    To simply get a timeout while waiting for an SMTP or ESMTP, use select(). It will indicate when there is data available on a socket, and it has a timeout parameter.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Use non-blocking sockets and a wait loop. Other than that, the ode you posted shoudl work fine, Your timeout value may just be very long.
    Last edited by abachler; 01-11-2008 at 02:10 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    Thank you all! I will see what way fits the best for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  3. Connecting to Server
    By osal in forum Networking/Device Communication
    Replies: 58
    Last Post: 06-10-2004, 10:10 AM
  4. Problems with sending data to a smtp server
    By knutso in forum Windows Programming
    Replies: 2
    Last Post: 04-01-2003, 07:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21