Im making a small set of socket classes and i was testing a piece of code to see if ReadLine() works. Here's the code that works:
Code:
TcpSocket sock;
if(sock.Connect(IpAddress::ResolveHost("www.google.com.au"), 80))
{
char recvBuff[4096];
sock.Write("GET / HTTP/1.1\n");
sock.Write("Host: www.google.com.au\n");
sock.Write("Connection: close\n");
sock.Write("\n");
while(sock.ReadLine(recvBuff, sizeof(recvBuff)))
printf("Line: %s\n", recvBuff);
}
return 1;
I figured it should also work if i change the size of "recvBuff" to 10, but it only receives part of the header (instead of both header and data) before the programs finishes. The output is this:
Code:
Line: HTTP/1.1
Line: 200 OK
Line: Cache-Con
Line: trol: pri
Line: vate, max
Line: -age=0
Line: Date: Wed
Line: , 11 Jun
Line: 2008 04:1
Line: 6:06 GMT
The read line code that i wrote is this:
Code:
bool soxTcpReadLine(unsigned int socketId, char* out, size_t outLen)
{
size_t recvCount = 0;
while(recvCount < outLen - 1)
{
char byte;
if(recv(socketId, &byte, 1, 0) > 0)
{
if(byte != '\n')
out[recvCount++] = byte;
else break;
} else break;
}
if(recvCount)
{
//Trim line
while(recvCount)
{
if(out[recvCount - 1] < ' ')
out[--recvCount] = 0;
else break;
}
//Null terminate
out[recvCount] = 0;
return true;
}
return false;
}