"nfiglesias@gmail.com" <nfiglesias@gmail.com> wrote:
Hello!
I'm very new on C and Winsock programming (i did some VB with winsock
control).
I've started with a little app. It works pretty well using an ECHO
server: i send a message, and i receive the same. Cool.
But, when i connect with a SMTP, POP or any other service, i receive
a first message, and then it hangs up.
...
int sendm(int socket){
FD_SET writeable;
FD_ZERO(&writeable);
FD_SET(socket, &writeable);
if(select(socket, 0, &writeable, 0, 0) != SOCKET_ERROR){
scanf("%s\r\n", request);
snd_b = strlen(request);
snd_b = send(socket, (char *)request, (snd_b + 1), 0);
return snd_b;
}else{return(0);}
}
The problem is your use of the dangerous "scanf" function. Because
of the way you have called this, the "request" variable will not have
a CR or LF at the end -- scanf will remove it. TCP services like
SMTP and POP always expect lines to end with a end-of-line sequence
(usually \r\n, although every server I've encountered accepts a lone
\n).
So, it's just like you telnetted into your SMTP server, and typed
HELO gmail.com
without pressing return at the end. You'd hang.
Use "fgets" instead of "scanf".
sending to the socket.