mikeeeblue
Programmer
Hi all.
Im trying to code a client to communicate with a smtp server. Upon connecting to the smtp server the server sends their informaiton to the client, so i need to use the recv() method to get this information. I use a char buffer of about 300 to read this information into. I then wish to send the server my commands from the client im programming and after every command use the recv() method to read the servers response, so it goes roughly like this
recv() //receive the server information
for(how many commands i want to send, 8)
send command
receive response
end for loop
The problem is is when i call the initial recv method i have space left over in the buffer, because it is set to 300 and the information the server sends is less than 300. So when i send my first command i cannot read the response correctly because it seems to carry on filling up the previously unfilled buffer. However if i set the original buffer to 50 (which is less than the amount sent by the server) then every time i do a recv() method it passes me the next part of the server info and not the response to the command. Anyone know how to get round this? Btw im using winsock and the code is below:
Im trying to code a client to communicate with a smtp server. Upon connecting to the smtp server the server sends their informaiton to the client, so i need to use the recv() method to get this information. I use a char buffer of about 300 to read this information into. I then wish to send the server my commands from the client im programming and after every command use the recv() method to read the servers response, so it goes roughly like this
recv() //receive the server information
for(how many commands i want to send, 8)
send command
receive response
end for loop
The problem is is when i call the initial recv method i have space left over in the buffer, because it is set to 300 and the information the server sends is less than 300. So when i send my first command i cannot read the response correctly because it seems to carry on filling up the previously unfilled buffer. However if i set the original buffer to 50 (which is less than the amount sent by the server) then every time i do a recv() method it passes me the next part of the server info and not the response to the command. Anyone know how to get round this? Btw im using winsock and the code is below:
Code:
//get server info
connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr));
int RetVal = SOCKET_ERROR;
char String[300];
while (RetVal == SOCKET_ERROR)
{
RetVal = recv(Socket, String, 301, 0);
if ((RetVal == 0)||(RetVal == WSAECONNRESET))
{
cout << ("Connection closed at other end.") << endl;
}
}
//send commands and get responses
for (int i = 0; i < 8; i++){
send(Socket, protocol[i], strlen(protocol[i]) + 1, 0);
cout << protocol[i] << endl;
int RetVal = SOCKET_ERROR;
while (RetVal == SOCKET_ERROR)
{
RetVal = recv(Socket, String, 301, 0);
if ((RetVal == 0)||(RetVal == WSAECONNRESET))
{
cout << ("Connection closed at other end.") << endl;
}
}
cout << String << endl;
}