Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Connectable (improved)
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2023-07-08 16:31:51
In general, if you require more advanced socket functionalities or need to work with different types of sockets (TCP, UDP, Unix domain sockets), stream_socket_client provides more flexibility.

However, if you have a simple use case and only need to establish a TCP/IP socket connection, fsockopen can be a more straightforward option.

Allows connection retry (3 times / peer), TLS options, async (non-blocking) and flexible.
This returns a string "yes" or "no".

PHP 7.4+

function getConnectable(string $ip, int $port): string
{
$options = array(
'ssl' => array(
'verify_peer' => true,
'verify_peer_name' => true,
'allow_self_signed' => false,
'disable_compression' => true
),
'socket' => array(
'backlog' => 128,
'bindto' => '0:0'
),
);

$context = stream_context_create($options);
$timeout = 5;

$retryAttempts = 3;
$retryDelay = 1; // in seconds

$connected = false;
$attempt = 1;

while (!$connected && $attempt <= $retryAttempts) {
$socketerr = "Attempt $attempt";
$socket = @stream_socket_client("tcp://$ip:$port", $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, $context);

if (is_resource($socket)) {
$sockets = array($socket);
$write = array($socket);
$except = array();

$numChanged = stream_select($sockets, $write, $except, $timeout);

if ($numChanged === false) {
$socketerr = "Error occurred during stream_select";
} else if ($numChanged === 0) {
$socketerr = "Connection timed out";
} else {
$connected = true;
$socketerr = "Connection successful";
}
} else {
$socketerr = "Connection failed: $errstr (Error $errno)";
}

if (!$connected && $attempt < $retryAttempts) {
sleep($retryDelay);
}

$attempt++;
}

// errors: $socketerr
return $connected ? 'yes' : 'no';
}


Usage:
$connectable = getConnectable($ip, $port);
Last edited by STorrents2020 on 2023-07-08 17:02:04
2023-07-09 18:57:27
up
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 2
  • Members: 0
  • Guests online: 2
  • Comments: 3
  • Messages: 1,728