Solved

socket timeout

Posted on 2006-11-23
6
1,149 Views
Last Modified: 2012-05-05
function connect($ip, $port) {
        global $sock;
        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($sock, "$ip", $port);
        switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 1))
        {
                case 1:
                        echo "Connected\n";
                        return 1;
                        break;
                case 0:
                        error("Unable to connect");
                        return;
                        break;
                case 2:
                        error("Unable to connect");
                        return;
                        break;
        }
        return;
}

I have this function to connect to a socket, but i want to have a timeout, if it doesen`t connects in 15 seconds to say Timeout
0
Comment
Question by:rares_dumitrescu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 18002436
see: http://de3.php.net/manual/en/function.socket-connect.php#56864 how to catch an timeout

global $sock;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock, "$ip", $port);
socket_set_block($sock);
switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5))
{
       case 2:
               echo "[-] Connection Refused\n";
               break;
       case 1:
               echo "[+] Connected\n";
               break;
       case 0:
               echo "[-] Timeout\n";
               break;
}
0
 
LVL 11

Expert Comment

by:ch2
ID: 18002511
You could also use fsockopen or pfsockopen for persistent connection, you can specify the timeout.

http://en.php.net/manual/en/function.pfsockopen.php
http://en.php.net/manual/en/function.fsockopen.php

You can specify the timeout. Look at the examples on the above links.

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 15);


Hope this helps
0
 
LVL 7

Expert Comment

by:jdpipe
ID: 18006568
How about this though?

http://au3.php.net/manual/en/function.stream-set-timeout.php

Example 1. stream_set_timeout() example
<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
   echo "Unable to open\n";
} else {

   fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
   stream_set_timeout($fp, 2);
   $res = fread($fp, 2000);

   $info = stream_get_meta_data($fp);
   fclose($fp);

   if ($info['timed_out']) {
       echo 'Connection timed out!';
   } else {
       echo $res;
   }

}
?>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question