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
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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

basically what I was saying, is that that headers get set to the wrong file.

 

It tries to set the headers for the server, not what is displayed in the browser.

 

Headers have nothing to do with a certain file. they are response headers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.