Lambneck Posted April 12, 2011 Share Posted April 12, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/ Share on other sites More sharing options...
requinix Posted April 12, 2011 Share Posted April 12, 2011 Comment out the set_time_limit (or better: lower it to, like, 5 seconds) and run your script. What happens? Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/#findComment-1200898 Share on other sites More sharing options...
Lambneck Posted April 13, 2011 Author Share Posted April 13, 2011 requinix I dont know whats going on. I tried as you suggested, both commenting it out and changing the time to 5. Either way there was no noticeable change in the result. Just the same blinking prompt. Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/#findComment-1200901 Share on other sites More sharing options...
gizmola Posted April 13, 2011 Share Posted April 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/#findComment-1200960 Share on other sites More sharing options...
requinix Posted April 13, 2011 Share Posted April 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/#findComment-1200974 Share on other sites More sharing options...
gizmola Posted April 13, 2011 Share Posted April 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233546-network-programming-here/#findComment-1200982 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.