Jump to content

[SOLVED] Socket Server


The Little Guy

Recommended Posts

In my following socket server, I am trying to listen to a connection through port 12345 in my web browser. http://127.0.0.1:12345/

 

I assumed that the following would detect that something was trying to connect to the port through the browser, using socket_create_listen. I don't know if I am doing something wrong or just not understanding what socket_create_listen is doing.

 

Does anyone know why when going to the address, my socket server isn't detecting the connection?

 

Note: The client is supposed to be the web browser, not another php file.

 

Here is my server class:

<?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($setup['server']['port'], $etup['server']['connections']);
		socket_listen($this->server);
	}
	function start(){
		while($client = socket_accept($this->server)){
			socket_write($client, "hello");
			print "here";
		}
	}
}
?>

 

Here is how I start the server:

<?php
include 'Pherver.php';
$server = new Pherver();
$server->start();
?>

 

Thank you for the help!

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

<?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($setup['server']['port'], $etup['server']['connections']);
		if(!$this->server){
			echo socket_last_error();
		}
		if(!socket_listen($this->server)){
			echo socket_last_error();
		}
	}
	function start(){
		while($client = socket_accept($this->server)){
			socket_write($client, "hello");
			print "here";
		}
	}
}
?>

 

 

OK added, some debugging, and it doesn't give any errors...

Link to comment
https://forums.phpfreaks.com/topic/179013-solved-socket-server/#findComment-944463
Share on other sites

yes I did I revised the previous one, and you may have missed it, but now I added some more debugging:

 

<?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($setup['server']['port'], $etup['server']['connections']);
		if(!$this->server){
			echo socket_last_error();
		}
		if(!socket_listen($this->server)){
			echo socket_last_error();
		}
	}
	function start(){
		while(TRUE){
			$client = socket_accept($this->server);
			if(!$client){
				echo socket_last_error();
			}
			socket_write($client, "hello");
			print "here";
		}
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/179013-solved-socket-server/#findComment-944467
Share on other sites

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.