473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
1 16563
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
2
by: jason | last post by:
Will this work - dynamic determination of root (local or web host) and consume this in include file anywhere. I am concerned about dynamic construction of virtual absolute include in the consuming...
3
by: Luis | last post by:
I am trying to write a program that checks to see if a fraction is reducable, and to do this both numerator and denominator cannot be tre( prime) will this work? if not can some one fix it up a bit...
3
by: Cool Mentalist | last post by:
function lightUp(light){ light.style.background="yellow" } hello hello2 hello3 etc etc
5
by: Mark Bruno | last post by:
I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does this work? : #include <iostream> using namespace std; int main() { cout << "hello, world" << endl;...
44
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
5
by: gabriele | last post by:
Hi, i'm new in this newsgroup, I'm working on a web based managerial software in my company and, for speed-up my work, have created a framework This framework uses the PHP and JAVASCRIPT...
2
by: Mark Cooney | last post by:
Hi Why doesnt this work? <% CatID = dsMain.SelectParameters("fkCatID").ToString%> or <% CatID = Eval("fkCatID") %> Anyone have a solution?
6
by: John | last post by:
Following code belongs to the first field in form B. When I open form B from form A, ctl.Name returns the name of Form's A button which I just clicked. How does this work? I thought when the field...
0
by: ml41782 | last post by:
I have this working but not as clean as it should be. I'm new to Perl and This has been my first project to monitor my solar equipment. Here is the data stream ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.