Thread: Problem implementing POP

  1. #1
    Unregistered
    Guest

    Problem implementing POP

    Hello,
    I'm writing a POP-client, and I have the following problem...
    When I connect to any mailserver on port 110, I get +OK and a welcome message. I do

    snprintf( buf, 100, "USER %s\r\n", username );
    send( s, buf, strlen( buf ) + 1, 0 );

    ...and I get another +OK, asking for my password.
    But when I do

    snprintf( buf, 100, "PASS %s\r\n" password );
    send( s, buf, strlen( buf ) + 1, 0 );

    ...i *always* get -ERR Unknown command: PASS. No matter which pop server I try, the PASS command will always work when I connect using telnet, but it always fails when using my client. What's wrong with this?
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    [code]
    snprintf( buf, 100, "PASS %s\r\n" password );
    send( s, buf, strlen( buf ) + 1, 0 );
    [\code]

    It would seem as if you've forgotten a comma between " "PASS %s\r\n" " and "password". Might this be the cause of your problems, or is it just a typo in your post?

    starX
    www.axisoftime.com

  3. #3
    Unregistered
    Guest
    Heh, just a typo, of course. Would it compile and get a reply from the server otherwise?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    I'm too lazy to write something to plug this into, but if you post your src file, I'll give it a shot.

    starX
    www.axisoftime.com

  5. #5
    Unregistered
    Guest
    Ok.

    Code:
    Includes: 
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    "pop.h":
    
    void receive( int sock, char *buf, int len )
    {
      ssize_t received = recv( sock, buf, len, 0 );
      int i;
      for( i = 0; i < received; i++ )
        putchar( buf[i] );
    }
    
    void pop( char *address, char *username, char *password )
    {
      char buf[100];
      int s = socket( AF_INET, SOCK_STREAM, 0 ),
          i;
      ssize_t received;
      struct hostent *ho;
      struct sockaddr_in remote;
      if( s < 0 )
      {
        printf( "%s\n", strerror( errno ) );
        return;
      }
      if( ( ho = gethostbyname( address ) ) == NULL )
      {
        printf( "%s\n", strerror( errno ) );
        return;
      }
      remote.sin_addr = *( struct in_addr* )ho->h_addr_list[0]; 
      remote.sin_port = htons( 110 );
      remote.sin_family = AF_INET;
      if( connect( s, ( struct sockaddr* )&remote, sizeof( struct sockaddr ) ) < 0)
      {
        printf( "%s\n", strerror( errno ) );
        return;
      }
      receive( s, buf, 100 );
      snprintf( buf, 100, "USER %s\r\n", username );
      send( s, buf, strlen( buf ) + 1, 0 );
      receive( s, buf, 100 );
      snprintf( buf, 100, "PASS %s\r\n", password );
      send( s, buf, strlen( buf ) + 1, 0 );
      receive( s, buf, 100 );
      send( s, "QUIT", 5, 0 );
      close( s );
    }
    Call it like pop( "mail.bleh.com", "foobar", "mypass" );

  6. #6
    Unregistered
    Guest
    Ok, figured it out- bleh.

  7. #7
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69
    always clear / reset your buffer with '/0' it's a safe practice

    try bzero/memcpy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM