LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2007, 08:34 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 31
PHP - can't get to sockets to communicate


I'm using the following server.php (most of it is taken directly from PHP docs):
Code:
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 10000;
$pass = "qazwsx";
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
        echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
        echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
        if (($msgsock = socket_accept($sock)) === false) {
                echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
                break;
        }
        do {
                if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
                        echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
                        break 2;
                }
                if (!$buf = trim($buf)) {
                        continue;
                }
                if (preg_match("/^$pass/",$buf)) {
                        $cmd = substr($buf,strlen($pass));
                        $out = `$cmd`;
                        socket_write($msgsock, $out, strlen($out));
                }
        } while (true);
        socket_close($msgsock);
} while (true);
socket_close($sock);
?>
And the following client.php:
Code:
<?php
error_reporting(E_ALL);

$service_port = 10000;

$address = '127.0.0.1';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
        echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
        echo "OK.\n";
}

$out = '';
$in = "qazwsxdf -h";
echo "Sending request...";
$er = socket_write($socket, $in, strlen($in));
if ($er == 0){
        print socket_strerror(socket_last_error($socket));
}
print "ER: $er\n";
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
        echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
However, the socket connects, and I'm getting only this message from the client:
Code:
OK.
Attempting to connect to '127.0.0.1' on port '10000'...OK.
Sending HEAD request...ER: 11
OK.
Reading response:
And it stays this forever. No talkback is done from the server. Actually it seems it doesn't recieve any input at all.

If I try to connect via telnet, and write the same string "qazwsxdf -h" everything is correct.
What am I doing wrong?
 
Old 11-07-2007, 08:56 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
you will need to terminate your line.
an inet socket is (generally)
line buffered.
so the server is waiting for the rest of the line or a terminator (T one thousand, liquid metal)

try appending a '\n' (or possibly a \r\n pair )
 
Old 11-08-2007, 05:54 AM   #3
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 31
You are 100% right.
I've added \n to the end of every line, and now they are proceeded correctly.
1 level up in life buffering
 
Old 11-08-2007, 06:54 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by ivanatora View Post
You are 100% right.
blimey, there's a first time for everything
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sockets In PHP baddah Programming 4 10-17-2007 08:35 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
communicate a php variable to a javascript function Kanaflloric Programming 4 08-11-2006 12:58 PM
How to communicate to IMAP/Cyrus server through SOCKETS? Claus Linux - Networking 4 10-31-2003 02:19 PM
PHP + Sockets Problem aspr Programming 0 06-04-2003 03:50 PM


All times are GMT -5. The time now is 09:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration