The correct and fastest path to Telnet in PHP. Sockets or cURL

Almost all PHP Telnet implementation examples have sockets (fsockopen). This does not work for me because it takes an invalid amount of time (~ 60 seconds).

I tried fsockopen for other purposes and found it slow, unlike cURL.

Question # 1: Why do sockets slow down?

Update: I found that we need to set stream_set_timeout , and we can control the socket runtime. I am wondering how to set the correct timeout or how to make it "stop waiting" after receiving a response.


I cannot get the same as cURL. Where should I put the commands that I need to send to telnet? Is CURLOPT_CUSTOMREQUEST selected correctly? I am doing something like this:

class TELNETcURL{ public $errno; public $errstr; private $curl_handle; private $curl_options = array( CURLOPT_URL => "telnet://XXX.XXX.XXX.XXX:<port>", CURLOPT_TIMEOUT => 40, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HEADER => FALSE, CURLOPT_PROTOCOLS => CURLPROTO_TELNET ); function __construct(){ $this->curl_handle = curl_init(); curl_setopt_array($this->curl_handle, $this->curl_options); } public function exec_cmd($query) { curl_setopt($this->curl_handle, CURLOPT_CUSTOMREQUEST, $query."\r\n"); $output = curl_exec($this->curl_handle); return $output; } function __destruct(){ curl_close($this->curl_handle); } } 

And something similar to this:

 $telnet = new TELNETcURL(); print_r($telnet->exec_cmd("<TELNET commands go here>")); 

I get a "Maximum Run Time Exceeding 30 Seconds" in the curl_exec command.

Question No. 2: What is wrong with the cURL implementation?

+4
source share
3 answers
Sockets

not slow. sockets are the basis for communication. Curl uses sockets to open a connection to a remote server. Everything works on sockets (I think). I don’t think you can use curl to use the telnet service, well, that’s not quite so, I think you can connect and send one command. Curl was designed with HTTP in mind, which is stateless (you open a connection, send a request, wait for a response, and close the connection). Sockets are the only option.

I get a "Maximum Run Time Exceeding 30 Seconds" in the curl_exec command.

I assume that the remote server is the culprit. check if it works using a regular client terminal, or increase max_execution_time in php ini.

UPDATE

It seems you can use curl for telnet, check this out: http://www.cs.sunysb.edu/documentation/curl/

But I still think you better use sockets.

+2
source

what you need to do is use NON-Blocking IO and then Poll to answer. what you are doing now is waiting / hanging for an answer that never comes - thus a timeout.

Personally, I wrote a lot of socket applications in php, they work great - and I hate cURL as buggies, bulky and very unsafe ... just read their list of errors, you should be shocked.

Go read the excellent PHP manual complete with many examples of how to do an IO survey, they even give you an example of a telnet server and client.

+3
source

use pfsockopen instead of fopensock .. it runs much faster and supports the connection to the end

0
source

Source: https://habr.com/ru/post/1385053/


All Articles