Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Sockets

Status
Not open for further replies.

plunkett

Technical User
Jun 10, 2004
57
US
Hi. I'm using IO::socket::INET to act as a client to connect to a chat server. My Problem is that when i'm waiting to get data from the socket, I can't do anyting else (like sending keep-alive message every two minutes to keep from getting disconnected!). Here is a piece of code to illistrate what i'm saying...

while (1)
{
$MySocket->recv($text,256);

if (($currenttime + 120) <= time()) # Used for keep-alive.
{
$currenttime = time();
$MySocket->send("Jp\n");
print "Ping?\n";
}
}

THe Conditional isn't reached until the socket receives data, which means that if the socket never receives data, nothing happens! Can anyone help?
 
Could you fork a keep alive child process?
--Paul
 
plunkett,

You can create a non-blocking mode connection like this
Code:
   $sock = IO::Socket::INET->new(Listen    => 5,
                                 LocalAddr => 'localhost',
                                 LocalPort => 9000,
                                 Blocking  => 0,
                                 Proto     => 'tcp');
As I understand the documentation this would make your call to recv() return immediately even if there was nothing to receive.

You can then go ahead with your check for a need for a keepalive thing, sleep for a short while then call recv() again in your loop.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
dang, for some reason that doesn't work..

It says bad file descriptor...
 
Oh :-( I got from the documentation as well. What version of Perl are you running? I'm on 5.8.1

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top