Jump to content

Network Programming Here


Lambneck

Recommended Posts

Hi all, I am trying to create a socket that can read from and write to a client. This is my first attempt at it and the code in question is based more or less on a python script a friend sent me as well as some php tutorials google supplied. The problem is (and this seems to be a reoccurring one) when I run it from the command line

php -q server.php

nothing visibly happens. Can anyone see what the problem might be?

 

<?php

$host = "localhost";
$port = 50007;

set_time_limit(0); // no timeout

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // create

$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // bind

$result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // listen

	echo "Waiting for connections...\n";

$contact = socket_accept($socket) or die("Could not accept incoming connection\n"); // accept

$input = socket_read($contact, 1024) or die("Could not read input\n"); // read

$input = trim($input); // clean

$output = "Greetings, you have connected to a socket.\n";
socket_write($contact, $output, strlen ($output)) or die("Could not write output\n"); // return

socket_close($contact); // close socket
socket_close($socket); // close socket

?>

 

Link to comment
https://forums.phpfreaks.com/topic/233546-network-programming-here/
Share on other sites

I have no idea what you're trying to do with php -q.  php -f scriptname.php is what you use to run a php command line script.

 

php -f server.php

 

The code works, for what little it does:

 

[david@penny ~]$ telnet localhost 50007

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

Hello

Greetings, you have connected to a socket.

Connection closed by foreign host.

 

 

 

I have no idea what you're trying to do with php -q.

It's a deprecated (but still supported) switch that means CLI mode. With older versions (like 4) PHP would spit out HTTP headers, even when run from the command line. The -q flag disabled that.

I have no idea what you're trying to do with php -q.

It's a deprecated (but still supported) switch that means CLI mode. With older versions (like 4) PHP would spit out HTTP headers, even when run from the command line. The -q flag disabled that.

 

So it does, interesting that it's not listed when you do php --help.

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.