By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

PHP Tcp Socket (I can't make this work)

P: n/a
I need a php page to connect to a python created socket and sent and
receive data. below is the python code which opens a socket on the
localhost @ port 21567:

#!/usr/bin/python2
from socket import *
from time import time,ctime

HOST ='127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpS = socket(AF_INET,SOCK_STREAM)
tcpS.bind(ADDR)
tcpS.listen(5)

while 1:
print "waiting for connection..."
tcpC ,addr = tcpS.accept()
print '...conected from:',addr

while 1:
data = tcpC.receive(BUFSIZ)
if not data: break
tcpC.send('[%s] %s' % (ctime(time()),data))
tcpC.close()
tcpS.close()

################## end of code

The above works fine I can attach via a python client on another machine.

Below is the php code for the page to attach to the socket at present
(for debugging) they are running on the same machine, it attaches and
sends the message "it is working" to the socket @ 21567 and it sould
return with the same message timestamped:

<?php
error_reporting (E_ALL);

echo "<h2>Socket Test</h2>\n";
$address = '127.0.0.1';
$service_port = 21567;
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0)
{
echo "socket_create() failed: reason: " . socket_strerror ($socket) .
"\n";
}
else
{
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$in = "is it working";
$out = '';
echo "Sending Info request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";echo "Reading response:\n\n";
while ($out = socket_read ($socket, 1024))
{
echo $out;
}

echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";

?>

below are the results:

Socket Test
OK. Attempting to connect to '127.0.0.1' on port '21567'...Sending Info
request...
Warning: socket_write() unable to write to socket 10 [32]: Broken pipe in
/var/www/html/tcpsock.php on line 20
OK. Reading response:
Warning: socket_read() unable to read from socket [107]: Transport
endpoint is not connected in /var/www/html/tcpsock.php on line 22
Closing socket...OK.
Any help would be greatly appreciated

CEC

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Chuck E. Cheese wrote:

well.. you haven't actually connected to anything yet
you need to put: socket_connect($socket, $address, $service_port);
in there before you try and write to the socket.

--Jeff
I need a php page to connect to a python created socket and sent and
receive data. below is the python code which opens a socket on the
localhost @ port 21567:

#!/usr/bin/python2
from socket import *
from time import time,ctime

HOST ='127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpS = socket(AF_INET,SOCK_STREAM)
tcpS.bind(ADDR)
tcpS.listen(5)

while 1:
print "waiting for connection..."
tcpC ,addr = tcpS.accept()
print '...conected from:',addr

while 1:
data = tcpC.receive(BUFSIZ)
if not data: break
tcpC.send('[%s] %s' % (ctime(time()),data))
tcpC.close()
tcpS.close()

################## end of code

The above works fine I can attach via a python client on another machine.

Below is the php code for the page to attach to the socket at present
(for debugging) they are running on the same machine, it attaches and
sends the message "it is working" to the socket @ 21567 and it sould
return with the same message timestamped:

<?php
error_reporting (E_ALL);

echo "<h2>Socket Test</h2>\n";
$address = '127.0.0.1';
$service_port = 21567;
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0)
{
echo "socket_create() failed: reason: " . socket_strerror ($socket) .
"\n";
}
else
{
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$in = "is it working";
$out = '';
echo "Sending Info request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";echo "Reading response:\n\n";
while ($out = socket_read ($socket, 1024))
{
echo $out;
}

echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";

?>

below are the results:

Socket Test
OK. Attempting to connect to '127.0.0.1' on port '21567'...Sending Info
request...
Warning: socket_write() unable to write to socket 10 [32]: Broken pipe in
/var/www/html/tcpsock.php on line 20
OK. Reading response:
Warning: socket_read() unable to read from socket [107]: Transport
endpoint is not connected in /var/www/html/tcpsock.php on line 22
Closing socket...OK.
Any help would be greatly appreciated

CEC


Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.