Jump to content

[SOLVED] Socket Server Response Headers


The Little Guy

Recommended Posts

Earlier I had a post about my Socket Server, I wasn't able to get it to connect, now I can :)

 

I am using socket_write to well hopefully send html to a web page, the problem I think is that I have to send Response Headers, but I am not sure how to do that. Is that what I need to do? Currently the page just sits there and loads, it connects to the sockets, but nothing ever shows up on the page, so that is why I think I also have to send response headers... Is that correct?

 

<?php
set_time_limit(0);
class Pherver{
	private $setup;
	public $server;
	public $connections;
	function __construct(){
		$this->setup = parse_ini_file("config.ini", TRUE);
		$this->server = socket_create_listen($this->setup['server']['port'], $this->setup['server']['connections']);
		if(!$this->server){
			echo socket_last_error();
		}
		if(!socket_listen($this->server)){
			echo socket_last_error();
		}
		echo "Pherver Service Started!\n";
		echo "Listening on port: ".$this->setup['server']['port']."\n";
	}
	function start(){
		while(TRUE){
			$client = socket_accept($this->server);
			if(!$client){
				echo socket_last_error();
			}else{
				$msg = "<html><body><h1>Hello</h1></body></html>";
				socket_write($client, $msg);
			}
		}
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/179017-solved-socket-server-response-headers/
Share on other sites

Yeah. You'll want to take a good look at the http protocol but a minimal response might look something like....

 

Content-Length: 40
Content-Type: text/html; charset=ISO-8859-1

200 OK

<html><body><h1>Hello</h1></body></html>

 

On a side not, the nanoweb project might interest you. Its a http server written completely in php. I've used it a few times in the past and its very responsive.

I've looked, and I am following their code ;) some

 

Content-Length: 40
Content-Type: text/html; charset=ISO-8859-1

200 OK

<html><body><h1>Hello</h1></body></html>

 

Along with socket_write, I am using an echo to echo out what is being sent to the function, and I am getting what you posted, but my browser still isn't displaying any thing... :confused:

Windows CMD... Same thing?

 

Not really, but yeah, your code is actually executed via php's cli, windows CMD can't actually execute anything but a limited set of built in's and other programs on its path.

 

Have you tried taking a look at the response headers within firefox's 'web developer' plugin?

Now I have to figure out why it is being displayed as plain text

 

I think you'll find the php cli is setup to send as plain text. You might want to take a look at your ini for the cli.

 

Actually, you should probably be using the header function to set these headers. That way you should be able to overide anything within the ini.

Now I have to figure out why it is being displayed as plain text

 

I think you'll find the php cli is setup to send as plain text. You might want to take a look at your ini for the cli.

 

Actually, you should probably be using the header function to set these headers. That way you should be able to overide anything within the ini.

 

Oh no, sorry, I ment it is plain/text in the browser.

 

It is displaying this in the browser:

Content-Length: 40
Content-Type: text/html; charset=ISO-8859-1

200 OK

<html><body><h1>Hello</h1></body></html>

but wouldn't that set the headers to the current file, instead of what is sent to the browser?

 

Sorry, that doesn't make much sense. Headers are headers. Your server needs to send them along with its content.

 

Really, this isn't something I've looked at doing before so...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.