Reading message from socket hangs

713 Views Asked by At

I'm trying to connect to a pop3 mail server using TCP, but when I try to read() the message right after connect() the console just hangs.

int sd;
struct sockaddr_in server;

if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror ("Error: socket()\n");
    return errno;
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ADDR);
server.sin_port = htons(PORT);

if (connect(sd, (struct sockaddr *) &server, sizeof(struct sockaddr)) == -1) {
    perror("Error: connect()\n");
    return errno;
}

char message[100];
read(sd, message, 100); // <== here it hangs
printf ("message: %s\n", message);

close (sd);

Where ADDR is the ip and PORT is the port of the server I'm trying to connect (in my case 188.125.69.47 and 995). From what I've read in the RFC, after connect() I should receive a following message:

+OK hello from ....
1

There are 1 best solutions below

1
On BEST ANSWER

Your read doesn't give you anything, because server is not sending any data. You're right that in POP3 server should send you a hello message first, but as you're connecting to POP3 over port 995, the SSL session needs to be established first. This however requires an initial handshake from the client.

You'll need a library to establish SSL connection and then talk in POP3, for example OpenSSL.

Answers in this question might be also helpful: How to use POP3 over SSL in C.