The Little Guy Posted October 26, 2009 Share Posted October 26, 2009 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); } } } } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 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... Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 You are running this script via the cli aren't you? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 Windows CMD... Same thing? Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 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? Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 Actually, HttpFox might be more helpful. There might be allot more headers involved than I originally thought. Your going to need to do quite a bit of reading on the http protocol I should imagine. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 YEY I got it! Thank you for your help! I was having issues because I wasn't closing the clients socket Now I have to figure out why it is being displayed as plain text :-\ Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 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> Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 Yeah, I thought that might happen. You need to use header instead of simply pre-pending the headers as text. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 but wouldn't that set the headers to the current file, instead of what is sent to the browser? Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 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... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 ha! your right... I was getting error message about headers already sent. Thanks, all fixed! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.