Sockets

Hey,

Firstly:

I'm trying to make a socket proxy server.

It will end up something like browser -> c++ -> PHP on a web server -> c++ -> browser.

So far I've got a program receiving the header requests from the browser. So step 1. The PHP bit is fine, I'm reasonably experienced there.

My question is:

Is there a specific order I need to do things in, and how can I go about sending the data from the PHP back to the browser?


I'm actually trying to send an html file (with faked headers) to the browser atm but with no success.

Function that gets the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
char* getFile() {
	  ifstream myReadFile;
	  myReadFile.open("text.txt");
	  char output[100];
	  if (myReadFile.is_open()) {
	  	 while (!myReadFile.eof()) {
		 	   myReadFile >> output;
         }
      }
	  myReadFile.close();

	  return output;     
}


Winsock command to send it to the browser:

 
send( ClientSocket, getFile(), 5000, 0 );


I don't know how to get the file size in bytes so I guessed with 5000.

This is the file I'm trying to send to the browser as the fake html page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HTTP/1.1 200 OK
Date: Tue, 13 Sep 2011 19:37:24 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 4307
Connection: close
Content-Type: text/html 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>thetree</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
hello
</body>
</html>



Am I doing this completely wrong?

If anyone can answer this I'd be extremely grateful.

Thanks

tree
Last edited on
Ok, I've managed to get the C++ to send the http file through the socket so the browser can load it. It works brilliantly until the file becomes larger than ~7300 bytes. Then it will just cut off the bottom.

I am sending a const char* through the socket send() function, could it be anything to do with max char size?

I'm thinking its a size limit somewhere and its just cutting the end off everything.

New file opening function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::string loadfile()
{
	std::string line;
	std::ifstream myFile;
	myFile.open("htmlfile.txt");
	if(!myFile.good())
	{
		std::cout << "Error opening file";
		return "ERROR";
	}
	
	std::string file;

	while(!myFile.eof())
	{		
		getline(myFile, line);
        file = file + line + "\r\n";	
	}
	myFile.close();

	return file;
}


If anyone can help me out I'd appreciate it.

Thanks

tree
Last edited on
Perhaps you could send the data in smaller pieces?
1
2
3
4
5
	while(getline(myFile, line)) // recommended
	{		
		// now you know that line is valid
		send(sock, line.c_str(), line.length(), 0); // send to socket
	}
Ahh, so you can send it in multiple parts?

I wasn't aware of this, thanks.

Do I need to send the http headers every time, or does the browser assume its part of the previous message?
The browser will assume it is part of the same message. The browser will not expect a new message until it sends a new HTTP request.
Topic archived. No new replies allowed.