Re: socket_create versus fsockopen
Ian.H [dS] wrote:
[color=blue]
> Ruben,
>
> I haven't tested socket_create() personally, but, if it's anything
> like Perls' IO::Socket module, I think you need to specify that the
> connection needs to be kept open in a loop.
>
> An an example from an IRC bot I coded, it looks something like:[/color]
[snip code]
[color=blue]
>
> This maintains a permanent connectio to the server. Although the
> code's slightly different to PHP's, this might offer some kind of
> insight =)[/color]
Hi Ian,
Thanks for the reply.
I get the idea of the loop, but it shouldn't be necessary in PHP. I
checked the docs, and it turns out to be normal that the connection is
broken after the end of the script, when using either socket_create and
socket_connect or fsockopen. However(!), when using pfsockopen, PHP is
supposed to create a persistent connection. But that doesn't quite work.
It behaves exactly like fsockopen; the connection is broken immediately
after the end of the script.
Basically what I want is to store the socket descriptor in my class
instance. I made a connection class, with some functions that send
commands to the server I connect to. One of the member variables of this
class is the socket descriptor. In my php session I store an instance of
this class, so I can send and receive data on the socket from every
script. But with this kind of behaviour I'd have to make a new
connection everytime and do the login sequence, and send the same
commands I did before, to get into the same state where I was before the
connection was broken. That's really not what I want. So my questions are:
* How can I keep a socket connection open?
* Why doesn't pfsockopen behave like the docs claim?
Thanks in advance.
Regards,
Ruben.
socket_create versus fsockopen
Collapse
This topic is closed.
X
X
-
Guest replied -
Guest repliedRe: socket_create versus fsockopen
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Whilst lounging around on Thu, 10 Jul 2003 11:37:28 +0200, Ruben van
Engelenburg <ruben@NOSPAMPL EASEtextinfo.nl > amazingly managed to
produce the following with their Etch-A-Sketch:
[color=blue]
> Hi all,
>
> I noticed that when I use socket_create and socket_connect that the
> socket goes into CLOSE_WAIT state immediately after receiving
> data. Even when I don't use socket_close on the socket resource.
> Another weird thing is that they stay in this state until the
> server gets booted. When I use fsockopen it works as expected, i.e.
> when I don't call fclose
> on the socket resource the socket state remains ESTABLISHED.
> I'm not sure if this has something to do with the fact that I tried
> it on a Windows server, but is this a bug, or is it supposed to
> behave like this?
>
> Regards,
> Ruben.[/color]
Ruben,
I haven't tested socket_create() personally, but, if it's anything
like Perls' IO::Socket module, I think you need to specify that the
connection needs to be kept open in a loop.
An an example from an IRC bot I coded, it looks something like:
## Real-time loop to maintain connection
while (1) {
&connect;
while (<SOCK>) {
# do bot stuff here
}
}
sub connect {
$sock = IO::Socket::INE T->new(
PeerAddr => $irc_server,
PeerPort => $irc_port,
Proto => 'tcp'
);
if (!$sock) {
print "Cannot connect to server: $irc_server\n";
sleep(60);
&connect;
}
}
This maintains a permanent connectio to the server. Although the
code's slightly different to PHP's, this might offer some kind of
insight =)
HTH.
Regards,
Ian
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0
iQA/AwUBPw08yGfqtj2 51CDhEQKdGQCg6M o1Hsv7ihm3tV38V 8SiJ2Qwqe0AnAwM
JGGM8gInZ6TLtF1 OTXDKTb8m
=pPiH
-----END PGP SIGNATURE-----
--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
Programming, Web design, development & hosting.
Leave a comment:
-
socket_create versus fsockopen
Hi all,
I noticed that when I use socket_create and socket_connect that the
socket goes into CLOSE_WAIT state immediately after receiving data. Even
when I don't use socket_close on the socket resource. Another weird
thing is that they stay in this state until the server gets booted.
When I use fsockopen it works as expected, i.e. when I don't call fclose
on the socket resource the socket state remains ESTABLISHED.
I'm not sure if this has something to do with the fact that I tried it
on a Windows server, but is this a bug, or is it supposed to behave like
this?
Regards,
Ruben.
Tags: None
Leave a comment: