note 84465 added to function.socket-connect

From: Date: Tue, 15 Jul 2008 17:04:14 +0000
Subject: note 84465 added to function.socket-connect
Groups: php.notes 
Request: Send a blank email to php-notes+get-141537@lists.php.net to get a copy of this message
Here is an example of a non-blocking connect which should perform quite a bit faster than the one posted by Seymour below: function msConnectSocket($remote, $port, $timeout = 30) { # this works whether $remote is a hostname or IP $ip = ""; if( !preg_match('/^\d+\.\d+\.\d+\.\d+$/', $remote) ) { $ip = gethostbyname($remote); if ($ip == $remote) { $this->errstr = "Error Connecting Socket: Unknown host"; return NULL; } } else $ip = $remote; if (!($this->_SOCK = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { $this->errstr = "Error Creating Socket: ".socket_strerror(socket_last_error()); return NULL; } socket_set_nonblock($this->_SOCK); $error = NULL; $attempts = 0; $timeout *= 1000; // adjust because we sleeping in 1 millisecond increments $connected; while (!($connected = @socket_connect($this->_SOCK, $remote, $port+0)) && $attempts++ < $timeout) { $error = socket_last_error(); if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) { $this->errstr = "Error Connecting Socket: ".socket_strerror($error); socket_close($this->_SOCK); return NULL; } usleep(1000); } if (!$connected) { $this->errstr = "Error Connecting Socket: Connect Timed Out After $timeout seconds. ".socket_strerror(socket_last_error()); socket_close($this->_SOCK); return NULL; } socket_set_block($this->_SOCK); return 1; } ---- Server IP: 216.92.131.65 Probable Submitter: 71.16.69.162 ---- Manual Page -- http://www.php.net/manual/en/function.socket-connect.php Edit -- https://master.php.net/note/edit/84465 Del: integrated -- https://master.php.net/note/delete/84465/integrated Del: useless -- https://master.php.net/note/delete/84465/useless Del: bad code -- https://master.php.net/note/delete/84465/bad+code Del: spam -- https://master.php.net/note/delete/84465/spam Del: non-english -- https://master.php.net/note/delete/84465/non-english Del: in docs -- https://master.php.net/note/delete/84465/in+docs Del: other reasons-- https://master.php.net/note/delete/84465 Reject -- https://master.php.net/note/reject/84465 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141537) next »