Creating listening socket in php. Initial issues

MadJuggla9

2[H]4U
Joined
Oct 9, 2002
Messages
3,515
I'm wanting a php program that runs on my machine that I can telnet to on a certain port and type commands to, possibly tie it into a shell or something later.

I've found 2 examples of code to start off but neither work. I tried enabling the sockets library extension and it still can't seem to find the libraries. I get these errors, ideas?:

Code:
C:\Users\chris\sockets>php listen_all.php

Fatal error: Call to undefined function socket_create_listen() in C:\Users\chris
\sockets\listen_all.php on line 3

C:\Users\chris\sockets>php socket_bind.php

Fatal error: Call to undefined function socket_create() in C:\Users\chris\socket
s\socket_bind.php on line 6

C:\Users\chris\sockets>



listen_all.php
Code:
 <?php

$socket = socket_create_listen("12345") or die();
echo "doesnt make it to here";
if (!$socket) {
	echo "Failed to create socket!\n";
	exit;
}

while (true) {
	$client = socket_accept($socket);
	$welcome = "\nWelcome to the server.\nType '!close' to close this connection, or type '!halt' to halt the server.\n";
	socket_write($client, $welcome);

	while (true) {
		$input = trim(socket_read ($client, 256));

		if ($input == '!close') {
			echo "input was !CLOSE\n";
			break;
		}
		if ($input == '!halt') {
			echo "input was !HALT\n";
			socket_close ($client);
			break 2;
		}

		$output = str_rot13($input) . "\n";
		socket_write($client, $output);
		print "Them: $input, Us: $output\n";
	}
	socket_close ($client);
	
}
socket_close ($socket);
?>


socket_bind.php
Code:
<?php
$address = "127.0.0.1";
$port = "12345";

/* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock, $address, $port);
socket_listen($mysock, 5);
$client = socket_accept($mysock);

// Read the input from the client &#8211; 1024 bytes
$input = socket_read($client, 1024);
$output = "thanks for connecting, you wrote: ".$input."\r\n";

// Display output back to client
socket_write($client, $output);
socket_close($client);
socket_close($mysock);
?>
 
Here's the first thing I would check:

The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.

http://us2.php.net/sockets

So I think you're on the right track thinking it's something wrong with the way you're using the extension. That being said, it's been years since I've looked at PHP and don't know the details of your setup. Have you gotten other extensions to work?
 
Here's the first thing I would check:



http://us2.php.net/sockets

So I think you're on the right track thinking it's something wrong with the way you're using the extension. That being said, it's been years since I've looked at PHP and don't know the details of your setup. Have you gotten other extensions to work?

Yes, I use gd2, zlib, and some other extensions. I'm on a windows box too. I've checked my php.ini that I always use and everything is proper.



a) don't use telnet
b) install an ssh daemon

It's a little side project to mess with and build on, don't hate :p
 
Yes, I use gd2, zlib, and some other extensions. I'm on a windows box too. I've checked my php.ini that I always use and everything is proper.

By everything being proper you mean the default

Code:
;extension=php_sockets.dll

Was uncommented? And that

Code:
extension_dir =

Points to the proper extension directory that contains php_sockets.dll?

That type of error message should only be happening if either A) php did not load the socket extension (see above), or B) there was an error while loading it. In which case, you should make sure

Code:
display_startup_errors =

is set to On.
 
By everything being proper you mean the default

Code:
;extension=php_sockets.dll

Was uncommented? And that

Code:
extension_dir =

Points to the proper extension directory that contains php_sockets.dll?

That type of error message should only be happening if either A) php did not load the socket extension (see above), or B) there was an error while loading it. In which case, you should make sure

Code:
display_startup_errors =

is set to On.

I don't mean proper by default, I mean I have manually added them before. Everything checked out exactly as you stated. I've checked my error logs but they say the same thing as my console output and nothing more. Just a call to an undefine function. Where should I be expecting a PHP startup error to occur?

Code:
;extension=php_shmop.dll
;extension=php_snmp.dll
extension=php_sockets.dll
;extension=php_sybase_ct.dll
;extension=php_tidy.dll


Code:
; Directory in which the loadable extensions (modules) reside.
extension_dir = "c:/server/php/ext/"


Code:
display_errors = On

; Even when display_errors is on, errors that occur during PHP's startup
; sequence are not displayed.  It's strongly recommended to keep
; display_startup_errors off, except for when debugging.
display_startup_errors = On

; Log errors into a log file (server-specific log, stderr, or error_log (below))
; As stated above, you're strongly advised to use error logging in place of
; error displaying on production web sites.
log_errors = On
 
TTT

No answer yielding google results yet, just same code that can't find the libraries.
 
Back
Top