I am having problems receiving data from a remote server. It seems that I can send an HTTP request to any host such as http://www.google.com, but I can't get a reply.
I'm sending the host a HTTP 1.0 request that is formatted as such:
GET / HTTP/1.0
Host: http://www.google.com
Connection: close
Here are is part of the proxy code:
//Parse Request Buffer
HttpRequest req;
req.ParseRequest(request.c_str(), request.length());
//Format Data to be Sent:
req.AddHeader ("Connection", "close"); // add connection close headers
req.SetVersion ("1.0"); // set http 1.0 flag
char *requestBuf = new char [req.GetTotalLength()];
req.FormatRequest(requestBuf);
//Get host IP by name
struct hostent *host;
host = gethostbyname(req.GetHost().c_str());
//Setup remote address structure
struct sockaddr_in remoteAddr;
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = htons(req.GetPort());
remoteAddr.sin_addr = *((struct in_addr *)host->h_addr);
//create new socket for remote server
int remoteSock = socket(AF_INET, SOCK_STREAM, 0);
if(remoteSock < 0)
{
perror("Socket Error");
//return -1;
}
//connecting to remote server
if(connect(remoteSock, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)) < 0)
{
perror("Connect Error");
//return -1;
}
cout.flush() << requestBuf << endl;
//send request to remote server
if(send(remoteSock, requestBuf, sizeof(requestBuf), 0) < 0)
{
perror("Write Error");
//return -1;
}
//receive reply from server
char *recBuf = new char [1024];
int bufSize = 1024;
int readSize;
while(1)
{
readSize = read(remoteSock, recBuf, bufSize); //Read from Remote Server
cout.flush() << recBuf << endl;
if(readSize <= 0) //If the requestBuf not filled, nothing left to send to client
break;
write(clientSock, recBuf, readSize); //write reply to Client
}
close(remoteSock);
The cout.flush() of recBuf outputs nothing.<br>
Any ideas on whats going on?