Sombody PLEASE help!! Connect returning -1

This is a discussion on Sombody PLEASE help!! Connect returning -1 within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that will send an email to a gmail account. I'm using VS and I'm ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Sombody PLEASE help!! Connect returning -1

    I'm trying to write a program that will send an email to a gmail account. I'm using VS and I'm on with Windows XP. I don't seem to understand why connect() is returning -1


    Code:
    # include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <winuser.h>
    #include <windowsx.h>
    #include <time.h>
    
    #define BUFSIZE 800
    #define waittime 500
    #define cmailserver "gmail-smtp-in.l.google.com"
    #define cemailto "atamrazian@gmail.com"
    #define cemailfrom "agentsmith@fbi.gov"
    #define SMTPLog "ring.wav"
    #define cemailsubject "Logged"
    using namespace std;
    int MailIt (char *mailserver, char *emailto, char *emailfrom,char *emailsubject, char *emailmessage);
    
    int main()
    {
    	MailIt(cmailserver,cemailto,cemailfrom,"test123","this is test");
    	return (0);
    }
    int MailIt (char *mailserver, char *emailto, char *emailfrom, 
    char *emailsubject, char *emailmessage) {
        SOCKET sockfd;
        WSADATA wsaData;
        FILE *smtpfile;
        
        #define bufsize 300
        int bytes_sent;   /* Sock FD */
        int err;
        struct hostent *host;   /* info from gethostbyname */
        struct sockaddr_in dest_addr;   /* Host Address */
        char line[1000];
        char *Rec_Buf = (char*) malloc(bufsize+1);
        smtpfile=fopen(SMTPLog,"a+");
        if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) {
          fputs("WSAStartup failed",smtpfile);
          WSACleanup();
          return -1;
        }
        if ( (host=gethostbyname(mailserver)) == NULL) {
           perror("gethostbyname");
           exit(1);
        }
        memset(&dest_addr,0,sizeof(dest_addr));
        memcpy(&(dest_addr.sin_addr),host->h_addr,host->h_length);
     
         /* Prepare dest_addr */
         dest_addr.sin_family= host->h_addrtype;  /* AF_INET from gethostbyname */
         dest_addr.sin_port= htons(25); /* PORT defined above */
     
         /* Get socket */
     
         if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0) {
            perror("socket");
            exit(1);
            }
         /* Connect !*/
         fputs("Connecting....\n",smtpfile);
     
        if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(dest_addr)) == -1){
    		int test=WSAGetLastError();
    		cout<<"Can not connect"<<endl;
            perror("connect");
            exit(1);
            }
    
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         strcpy(line,"helo me.somepalace.com\n");
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         strcpy(line,"MAIL FROM:<");
         strncat(line,emailfrom,strlen(emailfrom));
         strncat(line,">\n",3);
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         strcpy(line,"RCPT TO:<");
         strncat(line,emailto,strlen(emailto));
         strncat(line,">\n",3);
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         strcpy(line,"DATA\n");
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         Sleep(waittime);
         strcpy(line,"To:");
         strcat(line,emailto);
         strcat(line,"\n");
         strcat(line,"From:");
         strcat(line,emailfrom);
         strcat(line,"\n");
         strcat(line,"Subject:");
         strcat(line,emailsubject);
         strcat(line,"\n");
         strcat(line,emailmessage);
         strcat(line,"\r\n.\r\n");
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         strcpy(line,"quit\n");
         fputs(line,smtpfile);
         bytes_sent=send(sockfd,line,strlen(line),0);
         Sleep(waittime);
         err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
         fputs(Rec_Buf,smtpfile);
         fclose(smtpfile);                          
         #ifdef WIN32
         closesocket(sockfd);
         WSACleanup();
         #else
         close(sockfd);
         #endif
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Tried your code - have no problem connecting...
    VC6 + Win XP

    Check that the port is not closed by your firewall
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by vart
    Tried your code - have no problem connecting...
    VC6 + Win XP

    Check that the port is not closed by your firewall

    did the program run fine all the way through or did you just try the connect() part?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I didn't tried all the way, just several send/ recv
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by vart
    I didn't tried all the way, just several send/ recv
    will it be possible for you to run the entire code. I want to see if I get an email at my gmail account or not. That way I'll know if its a code error or its just my machine.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Ran it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by vart
    Ran it
    ok thanks a lot it sent me the email

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    Well this is in a world-o-trouble if recv returns -1

    > int test=WSAGetLastError();
    a) output the error number then
    b) read the manual page on getlasterror and find out how to get the text description for that error.
    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
    Jan 2007
    Posts
    8
    Quote Originally Posted by Salem
    > err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
    Well this is in a world-o-trouble if recv returns -1

    > int test=WSAGetLastError();
    a) output the error number then
    b) read the manual page on getlasterror and find out how to get the text description for that error.
    the error is 10060 which is time-out... it has to be something on my machine because vart tried it on his machine and i got the email shortly. Cant figure out whats going on differently on my computer?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Does your normal email client work?
    Have you compared ethereal traces of a normal email program and 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.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by Salem
    Does your normal email client work?
    Have you compared ethereal traces of a normal email program and your program?
    outlook sends emails to gmail just fine

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    OK, so compare ethereal traces.
    Where it starts to be different is where you should be looking.

    Does your firewall restrict only outlook to sending email?
    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.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    try telnet. it seems it wont let me connectn either. its not a bug in your code, maybe the settings arent right? maybe its a possibility that gmail-smtp-in.l.google.com isnt good?

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    i shut off firewall code still didn't send email...tried telnet but telnet failed to send!!...ill look into it (must be internal setting) and post again when i find the solution. Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  4. socket connect returning odd
    By WaterNut in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:49 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 02:46 PM

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