This is a discussion on Help with a socket connection? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Can someone tell me what is wrong with this script? I'm trying to communicate with another process using a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Can someone tell me what is wrong with this script?
I'm trying to communicate with another process using a socket connection. This script works in the sense that it creates the socket, sends its message ("Hello World") and receives the message from the other process. But the "While loop" that receives the message never completes. After 30 seconds it times out, and then the script completes. This is not actually a "file" that's being read - is that the problem? What, exactly, is "feof" looking for? If it makes any difference, I using PHP 5, IIS 5 on WinXP Help? <?php $portnum = 1001; $address = "192.168.200.19"; $socket=fsockopen($address, $portnum); $out = "Hello World"; fwrite($socket,$out); while(!feof($socket)){ echo fgets($socket); } fclose($socket); ?> |
|
|||
|
On 7 Sep 2004 08:10:55 -0700, "Steve" <googlespam@nastysoft.com>
wrote: > >See other posts here, and in comp.lang.php, on similar socket-related >issues. Check that the socket server process is sending a CR/LF pair >("\r\n") otherwise no EOF will be detected by the client. > >--- >Steve I have checked other posts. I've read other newsgroups. I've studied php.net stuff. I've been trying to get this to work for several days now. When you say "\r\n", do you mean literally those characters? Or do you mean ASCII char 13 and ASCII char 10? I have tried all of these in various combinations. I can't get anything to work. (I've also tried ASCII char 26 which I believe is a commonly used EOF marker). The socket server process is my own program (written in VB6) so I can have it send whatever needs to be sent. Any additional suggestions will be greatly appreciated. |
|
|||
|
Hello,
On 09/07/2004 11:22 AM, Martin wrote: > Can someone tell me what is wrong with this script? > > I'm trying to communicate with another process using a socket > connection. This script works in the sense that it creates the socket, > sends its message ("Hello World") and receives the message from the > other process. But the "While loop" that receives the message never > completes. After 30 seconds it times out, and then the script > completes. > > This is not actually a "file" that's being read - is that the problem? > What, exactly, is "feof" looking for? feof will only return true when the connection is closed because the server closed it or there was a networking error. If the server does not close the connection, you will remain in an infinite loop. -- Regards, Manuel Lemos PHP Classes - Free ready to use OOP components written in PHP http://www.phpclasses.org/ PHP Reviews - Reviews of PHP books and other products http://www.phpclasses.org/reviews/ Metastorage - Data object relational mapping layer generator http://www.meta-language.net/metastorage.html |
|
|||
|
On Tue, 07 Sep 2004 15:01:09 -0300, Manuel Lemos <mlemos@acm.org>
wrote: >Hello, > >On 09/07/2004 11:22 AM, Martin wrote: >> Can someone tell me what is wrong with this script? >> >> I'm trying to communicate with another process using a socket >> connection. This script works in the sense that it creates the socket, >> sends its message ("Hello World") and receives the message from the >> other process. But the "While loop" that receives the message never >> completes. After 30 seconds it times out, and then the script >> completes. >> >> This is not actually a "file" that's being read - is that the problem? >> What, exactly, is "feof" looking for? > >feof will only return true when the connection is closed because the >server closed it or there was a networking error. If the server does not >close the connection, you will remain in an infinite loop. Thanks. Can you suggest a way that I can read in this data and have it stop reading when I've reached the end of the message? |
|
|||
|
Hello,
On 09/07/2004 03:09 PM, Martin wrote: >>>Can someone tell me what is wrong with this script? >>> >>>I'm trying to communicate with another process using a socket >>>connection. This script works in the sense that it creates the socket, >>>sends its message ("Hello World") and receives the message from the >>>other process. But the "While loop" that receives the message never >>>completes. After 30 seconds it times out, and then the script >>>completes. >>> >>>This is not actually a "file" that's being read - is that the problem? >>>What, exactly, is "feof" looking for? >> >>feof will only return true when the connection is closed because the >>server closed it or there was a networking error. If the server does not >>close the connection, you will remain in an infinite loop. > > > Thanks. > > Can you suggest a way that I can read in this data and have it stop > reading when I've reached the end of the message? If you developed the server, just close the connection. -- Regards, Manuel Lemos PHP Classes - Free ready to use OOP components written in PHP http://www.phpclasses.org/ PHP Reviews - Reviews of PHP books and other products http://www.phpclasses.org/reviews/ Metastorage - Data object relational mapping layer generator http://www.meta-language.net/metastorage.html |
|
|||
|
On Tue, 07 Sep 2004 15:30:33 -0300, Manuel Lemos <mlemos@acm.org>
wrote: >Hello, > >On 09/07/2004 03:09 PM, Martin wrote: >>>>Can someone tell me what is wrong with this script? >>>> >>>>I'm trying to communicate with another process using a socket >>>>connection. This script works in the sense that it creates the socket, >>>>sends its message ("Hello World") and receives the message from the >>>>other process. But the "While loop" that receives the message never >>>>completes. After 30 seconds it times out, and then the script >>>>completes. >>>> >>>>This is not actually a "file" that's being read - is that the problem? >>>>What, exactly, is "feof" looking for? >>> >>>feof will only return true when the connection is closed because the >>>server closed it or there was a networking error. If the server does not >>>close the connection, you will remain in an infinite loop. >> >> >> Thanks. >> >> Can you suggest a way that I can read in this data and have it stop >> reading when I've reached the end of the message? > >If you developed the server, just close the connection. Thanks again but there's gotta be a better way. I tried what you said, but when I close the connection from the server end (immediately after sending the data), the script completes without a delay but the data still does not show up (that is, the echo displays no data)(I'm guessing that the feof detects the closed connection before the fgets can do its work). I found that if I delay the closing of the connection (but still close it from the server end), then the data does show up but that's kind of a hokey way of doing it in my opinion. Can anyone tell me if there is some way in php to read in the data string and, upon receipt of some specific end-of-data marker, stop reading and close the connection? And keep in mind that this is a socket connection not a file. I've tried fread, fgets, fgetc - I can't get any of them to work with the socket connection. I don't know if they are supposed to or not as I can't find any examples and I don't know enough about php to do much experimenting |
|
|||
|
On Tue, 07 Sep 2004 07:22:37 -0700, Martin <martinvalley@comcast.net>
wrote: >Help? > OK - finally got this working the way I wanted. If this is of any use to anyone, help yourself. <?php $portnum = 1001; $address = "192.168.200.19"; $socket=fsockopen($address, $portnum); stream_set_timeout($socket, 2); /* this is optional $out = "Hello World"; fwrite($socket,$out); $in = ""; while (($in = fgetc($socket))==true ) { if($in==chr(13)) { /* use whatever terminating char you want fclose($socket); exit; } echo $in; } ?> |