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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.