473,756 Members | 8,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(BU FSIZ)
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";ech o "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'...Sendi ng 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 16618
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(BU FSIZ)
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";ech o "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'...Sendi ng 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
3362
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 Order By Company ASC";
2
2248
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 file. Take a look and tell me what you think: GLOBAL.ASA Sub application_onstart
3
1953
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 for me? thanks. # include <iostream.h> using namespace std; int main()
3
1621
by: Cool Mentalist | last post by:
function lightUp(light){ light.style.background="yellow" } hello hello2 hello3 etc etc
5
1607
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; system("PAUSE"); return 0;
44
3279
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
1673
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 technology. the innovation in this work is the approach:
2
1464
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
1807
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 got focus that would be the active control. How can I solve it so that it will return the name of the field the code is in? Private Sub MyFirstField_GotFocus() Dim ctl As Control Set ctl = Screen.ActiveControl
0
952
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 {"result":{"overview":},"version":"1.0","format":"JSON","proc":"GetPlantOverview","id":"1"} Here is my code. It give it to me in a decent view. lookin to find out how much easier it would have been to make this work in a table format and how to get it to auto refresh.
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.