Just for comparison, this is the greeting function that I am currently using in my SMTP library (C++).
Code:
void SmtpSocket::sendGreeting()
{
const std::string greeting_command = "EHLO";
size_t greeting_msg_len;
const std::string crlf = "\r\n";
int numbytes;
char buf[MAXRECVSIZE];
std::string received;
std::ostringstream greeting_msg;
fd_set listener;
struct timeval timeout;
/* set the timeout for recieving all the data */
timeout.tv_sec = 2; /* seconds */
timeout.tv_usec = 0; /* microsecods */
// The greeting message is simply just a 'EHLO domain.name'
greeting_msg << greeting_command << " webmastermattd.net" << crlf;
greeting_msg_len = greeting_msg.str().length();
// Now it is time to send the greeting on its way
if((numbytes = send(sockfd, greeting_msg.str().c_str(), greeting_msg_len, 0)) == -1)
throw SmtpSocketException("Unable to send greeting to server");
// Now clear the buffer and prepare it to recieve the data
memset((char *)&buf, '\0', MAXRECVSIZE);
if ((numbytes = recv(sockfd, buf, MAXRECVSIZE-1, 0)) == -1) {
throw SmtpException("unable to recieve");
}
received.insert(received.length(), (char *)&buf[0]);
if (numbytes >= MAXRECVSIZE-1) {
/* Prepare the select stuff */
FD_ZERO(&listener);
FD_SET(sockfd, &listener);
while(1) {
if (select(sockfd + 1, &listener, NULL, NULL, &timeout) == -1) {
break;
}
if(!FD_ISSET(sockfd, &listener))
break;
memset(&buf, '\0', MAXRECVSIZE);
if ((numbytes = recv(sockfd, buf, MAXRECVSIZE-1, 0)) == -1) {
throw SmtpException("unable to recieve!!!");
break;
}
received.insert(received.length(), (char *)&buf[0]);
}
}
// Now for a simple bit of validation
if( received.find("250") == std::string::npos )
throw SmtpInvalidServerException("Server did not respond to the greeting correctly");
} // Thus ends the SmtpSocket sendGreeting function, it takes no parameters
The only differences is that I am using C++ and that I send a CRLF instead of a simple LF as a termination for the string. I would try to create the string outside of the send function and then send them using seperate functions. It seems that you are trashing the strings to some extent.
As far as receiving multiple lines from the SMTP server, then it is just a matter of a single call to recv() with a sufficient buffer size or multiple calls to recv() and then working with the strings to an extent. It isn't the CRLF or LF that determines the end of the string, rather the \0 that determines it.
Hope this helps, and feel free to bug me about any futher questions.
Later,