I am a PHP newbie (just got my "Hello World" page working this
morning). I'm doing some R&D work to see if PHP is viable for a
situation I have. To accomplish what I want to do, I have to have the
PHP page communicate directly with another process.
I want the PHP script to establish a socket connection to the other
process, send a message and receive some data back which would then
used for calculations and/or display on the resulting page. The PHP
page will be the client - the other process will be the server. The
server process is a program that I've written using Visual Basic 6 and
the socket there is the standard Winsock that's part of VB.
Shown below is the entire page that I'm using to test this concept;
I've adapted it from an example provided in the "Socket Functions"
section of the manual on the PHP.net website. Amazingly enough, this
actually works - up to a point. I'm hoping someone here can help get
it working all the way through.
This script works up to the point of reading the response. It creates
the connection (my server process accepts the connection and receives
the "Hello world" message). The server sends its message. But then the
script seems to just sit there - it doesn't complete - it doesn't
generate any output.
Can anyone suggest what I might need to do to get this to work?
If it matters, I'm using PHP 5.0.1, IIS 5.1 running on Windows XP with
service pack 2.
Thanks
-----------------------------------------------------------------------------------
<HTML>
<BODY>
<?php
echo "<h2>TCP/IP Connection</h2>\n";
$service_port = 1001;
$address = "192.168.200.19 ";
$socket = socket_create(A F_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create( ) failed: reason: " . socket_strerror ($socket)
.. "<br>";
} else {
echo "OK.<br>";
}
echo "Attempting to connect to '$address' on port '$service_port' ...";
$result = socket_connect( $socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect () failed.\nReason : ($result) " .
socket_strerror ($result) . "<br>";
} else {
echo "OK.<br>";
}
$in = "Hello World";
$out = "";
echo "Sending message...";
socket_write($s ocket, $in, strlen($in));
echo "OK.<br>";
echo "Reading response: <br><br>";
while ($out = socket_read($so cket, 999, PHP_NORMAL_READ )) {
echo $out;
}
echo "<br>Closin g socket...";
socket_close($s ocket);
echo "OK.<br><br >";
?>
</BODY>
<HTML> 7 6370
On Thu, 02 Sep 2004 15:40:13 -0700, Martin <ma**********@c omcast.net> wrote: This script works up to the point of reading the response. It creates the connection (my server process accepts the connection and receives the "Hello world" message). The server sends its message. But then the script seems to just sit there - it doesn't complete - it doesn't generate any output.
What does the server do after it's sent "Hello world"?
Does it follow this with a \n (since you're using the PHP_NORMAL_READ option
which claims to stop reading when it hits \r or \n), or close the connection?
If it doesn't do either of those, your script is probably just waiting for the
server to send some more data (you asked for 999 bytes).
--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Martin <ma**********@c omcast.net> wrote: This script works up to the point of reading the response. It creates the connection (my server process accepts the connection and receives the "Hello world" message). The server sends its message. But then the script seems to just sit there - it doesn't complete - it doesn't generate any output.
Are you sure it stops at the read? You are not checking if the write
succeeds. Have you used a packetsniffer to find out whats really going
on?
Can anyone suggest what I might need to do to get this to work?
If it matters, I'm using PHP 5.0.1, IIS 5.1 running on Windows XP with service pack 2.
Mayeb you found another program that is broken by SP2 :)
echo "Reading response: <br><br>"; while ($out = socket_read($so cket, 999, PHP_NORMAL_READ )) { echo $out; }
This blocks untill:
- 999 bytes are read.
- a \n, \r or \0 is received
Are these conditions met?
BTW why socket_* and not fsockopen?
--
Daniel Tryba
Andy / Daniel -
Thanks. Mayeb you found another program that is broken by SP2 :)
yeah, very well could be. echo "Reading response: <br><br>"; while ($out = socket_read($so cket, 999, PHP_NORMAL_READ )) { echo $out; }
This blocks untill: - 999 bytes are read. - a \n, \r or \0 is received
Are these conditions met?
No, they weren't. I didn't know I needed to do that. When I added \n
to the message from the server, the script started working all the way
through. However, I'm still getting an error on the socket_read. It
says: "unable to read from socket. operation completed successfully."
I tried taking the socket_read out of the "while" construct but it
didn't make any difference.
I found a comment on the PHP.net site that said that socket_read
doesn't work in Windows (the comment is nearly a year old). The writer
says to use: socket_recv instead. I tried it thus:
$rcd=socket_rec v($socket,$buff er,999,0);
echo $rcd;
echo "<br>" . $buffer;
It worked!
Howeer, this appears to NOT wait on any terminating character. Since
it's possible for tcp/ip to break up the messages, I'm concerned that
I'll always receive complete messages. (I've had problems with that in
the past).
BTW why socket_* and not fsockopen?
Because I don't know any better. Should I be using it?
Any other advice and guidance will be greatly appreciated.
Thanks again.
Martin <ma**********@c omcast.net> wrote: Mayeb you found another program that is broken by SP2 :) yeah, very well could be.
It did remove atleast some socket support (raw IIRC).
[...] $rcd=socket_rec v($socket,$buff er,999,0); echo $rcd; echo "<br>" . $buffer;
It worked!
Howeer, this appears to NOT wait on any terminating character. Since it's possible for tcp/ip to break up the messages, I'm concerned that I'll always receive complete messages. (I've had problems with that in the past).
Huh? I guess you mean incomplete messages? You'll have to play with
timeouts and blocking mode... BTW why socket_* and not fsockopen?
Because I don't know any better. Should I be using it? http://www.php.net/manual/en/ref.sockets.php says:
This extension is EXPERIMENTAL. The behaviour of this extension --
including the names of its functions and anything else documented about
this extension -- may change without notice in a future release of PHP.
Use this extension at your own risk.
And since you are using a plain tcp socket, you might as wel use the
proven methods, which are fsockopen(), feof(), fread/fwrite,
fgets/fputs. Your program would roughly be (no error handling):
<?php
$s=fsockopen($a ddress, $service_port);
fwrite($s,"Hell o World");
while(!feof($s) )
{
echo fgets($s,999);
}
fclose($fp);
?>
--
Daniel Tryba
Martin wrote: I am a PHP newbie (just got my "Hello World" page working this morning). I'm doing some R&D work to see if PHP is viable for a situation I have. To accomplish what I want to do, I have to have the PHP page communicate directly with another process.
I want the PHP script to establish a socket connection to the other process, send a message and receive some data back which would then used for calculations and/or display on the resulting page. The PHP page will be the client - the other process will be the server. The server process is a program that I've written using Visual Basic 6 and the socket there is the standard Winsock that's part of VB.
Shown below is the entire page that I'm using to test this concept; I've adapted it from an example provided in the "Socket Functions" section of the manual on the PHP.net website. Amazingly enough, this actually works - up to a point. I'm hoping someone here can help get it working all the way through.
This script works up to the point of reading the response. It creates the connection (my server process accepts the connection and receives the "Hello world" message). The server sends its message. But then the script seems to just sit there - it doesn't complete - it doesn't generate any output.
Can anyone suggest what I might need to do to get this to work?
If it matters, I'm using PHP 5.0.1, IIS 5.1 running on Windows XP with service pack 2.
Thanks
--------------------------------------------------------------------------
--------- <HTML> <BODY>
<?php echo "<h2>TCP/IP Connection</h2>\n";
$service_port = 1001; $address = "192.168.200.19 ";
$socket = socket_create(A F_INET, SOCK_STREAM, SOL_TCP); if ($socket < 0) { echo "socket_create( ) failed: reason: " . socket_strerror ($socket) . "<br>"; } else { echo "OK.<br>"; }
By the way, you can use this construction as well for testing and stuff, as
many of the PHP functions return FALSE when failed.
Very common, easier to maintain too.
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die
socket_strerror ($socket);
echo "OK <br>";
Pjotr Wedersteers <x3****@westert erp.com> wrote: By the way, you can use this construction as well for testing and stuff, as many of the PHP functions return FALSE when failed. Very common, easier to maintain too.
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die socket_strerror ($socket); echo "OK <br>";
While this example might (accidentally) work for socket_create. Consider
the following example when trying to handle false.
$hay="foo bar";
$needle="foo";
if(strpos($hay, $needle)==false )
{
die("Fatal error: '$hay' doens't contain '$needle'");
}
vs.
if(strpos($hay, $needle)===fals e)
{
die("Fatal error: '$hay' doens't contain '$needle'");
}
Moral: if you know what type the return value should be, check for type
as well.
--
Daniel Tryba
On Fri, 3 Sep 2004 01:42:36 +0000 (UTC), Daniel Tryba
<ne************ ****@canopus.nl > wrote: Howeer, this appears to NOT wait on any terminating character. Since it's possible for tcp/ip to break up the messages, I'm concerned that I'll always receive complete messages. (I've had problems with that in the past). Huh? I guess you mean incomplete messages? You'll have to play with timeouts and blocking mode...
Yes, I meant *incomplete* messages. In my work with socket
communications, I've always used delimiters at both ends of the
messages. On the receiving end, I then build a string of received data
until I see the termination character at which point I do whatever
processing that needs to be done.
<snip>
And since you are using a plain tcp socket, you might as wel use the proven methods, which are fsockopen(), feof(), fread/fwrite, fgets/fputs. Your program would roughly be (no error handling):
<?php $s=fsockopen($ address, $service_port);
fwrite($s,"Hel lo World"); while(!feof($s )) { echo fgets($s,999); } fclose($fp); ?>
Thanks for the example.
Do you know if this works in Windows? I have not been able to get it
to work. PHP.net gives a very similar example but I simply cannot get
it to receive any data. I've added a bunch of error handling - I know
the socket is opening ok and it's sending the request message to my
other process. The other process is sending its response (I've tried
both terminated and non-terminated). No luck.
Any suggestions?
Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: JatP |
last post by:
hi Everyone
I am trying to create a server and client to send files from one side
to the other. I can send files from one side to the other using
bufferedinput/output streams but when trying to send normal messages
with printwriter my files being sent across are messed up. Empty jpg's
are being sent with wrong file sizes. My code as follows:
public class TCPServer
{
|
by: Robert Smith |
last post by:
As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me :)
When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.
I don't know what that means or why I am getting it.
I know the connection is made by the server: message.
|
by: Marty |
last post by:
Hi,
1-I am getting this error and I don't know what does it mean, can
anybody help me with the meaning of this?
error C2850: 'PCH header file' : only allowed at file scope; may not
be in a nested construct
Thank you very much.
|
by: ranishobha21 |
last post by:
Dear all,
i want to send some unix commands to remote unix machine in france
through php.i am using socket communication in php, i have written a
socket communication program so that it connects to the remote
machine.
i dont get any errors it says socket is connected.i dont know whether
the socket is connected or not.
|
by: cybervigilante |
last post by:
I can't seem to change the include path on my local winmachine no
matter what I do. It comes up as
includ_path .;C:\php5\pear in phpinfo() but there is no such file. I
installed the WAMP package and PEAR is in c:\wamp\php\pear
I modified php.ini in the c:\wamp\php directory to reflect the actual
path, but even stopping and restarting my server shows the c:
\php5\pear path. I can't change it no matter what I do I also tried
the...
| |
by: manontheedge |
last post by:
I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever was typed it. My problem is that this "client" code receives ONE message for the "server" and won't receive any more than that. I'm lost at this point. I've overcommented the code intentionally and took out all the error codes, because I wasn't...
|
by: zaina |
last post by:
hi everybody
i am nwebie in this forum but i think it is useful for me and the member are helpful
my project is about connecting client with the server to start exchanging messages between them.
to be more clear we process this purpose we serve this to the student in the university. how??
student will send a message that contains his name,id and request by format the server want such as zaina-20024008-grade.
the grade is the request...
|
by: Ahmed, Shakir |
last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server:
Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve this error and extract emails from an exchange server.
First I tried:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\lib\poplib.py", line 96, in __init__
raise socket.error, msg
|
by: brendanmcdonagh |
last post by:
Hi all,
I have my little message program working until i press a button and then it'll go to actionPerformed method, and stop reading but will keep sending a message which is being received. Obviously, this is because the send is in the actionperformed method but the read is in the main method. Anyone offer any solutions to getting my program reading again after actionmethod() has been called. I ve tried to just create a method for read which...
|
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,...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
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...
| |