-
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.
-
[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
-
Heh, just a typo, of course. Would it compile and get a reply from the server otherwise?
-
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
-
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" );
-
Ok, figured it out- bleh.
-
always clear / reset your buffer with '/0' it's a safe practice
try bzero/memcpy