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(AF_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($socket, $in, strlen($in));
echo "OK.<br>";
echo "Reading response: <br><br>";
while ($out = socket_read($socket, 999, PHP_NORMAL_READ)) {
echo $out;
}
echo "<br>Closing socket...";
socket_close($socket);
echo "OK.<br><br>";
?>
</BODY>
<HTML> 7 6349
On Thu, 02 Sep 2004 15:40:13 -0700, Martin <ma**********@comcast.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.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Martin <ma**********@comcast.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($socket, 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($socket, 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_recv($socket,$buffer,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**********@comcast.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_recv($socket,$buffer,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($address, $service_port);
fwrite($s,"Hello 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(AF_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****@westerterp.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)===false)
{
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,"Hello 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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: 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,...
|
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,...
|
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...
|
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...
|
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...
| |