TemKa_SD Posted April 30, 2019 Share Posted April 30, 2019 Здравствуйте. Написал вот такие функции: protected function checkPort($port) { $connection = @fsockopen( "212.109.217.193", $port, $errno, $errstr ); if ( $errno != "111") { $port = $port + 1; } return $port; } protected function getBotPort() { $lastport = \IPS\Db::i()->select( 'MAX(cfg_value)', static::$cfgTable, array('cfg_name=?', 'bot_hostport') )->first(); if ($lastport) { $port = $lastport + 1; } else { $port = $port; } return $this->checkPort(6116); } В форме стандартное значение функция getBotPort(). Логика следующая: Выбираем порты которые уже есть в базе данных, добавляем + 1 и возвращаем результат в функцию проверки порта на свободность. Есть порт занят - сейчас я еще просто +1 добавил, но нужно сделать перебор наверно, пока не найдет свободный порт. Подскажите, с чего начать? Это же foreach нужен? Link to comment Share on other sites More sharing options...
siv1987 Posted April 30, 2019 Share Posted April 30, 2019 Нет, foreach для обхода массива (или iterable объектов) а тут нужен while - выполнять цикл пока условие не будет истинное. protected function checkPort($port) { $connection = @fsockopen( "212.109.217.193", $port, $errno, $errstr ); return ( $errno == "111" ) ? $port : false; } protected function getBotPort() { $lastport = \IPS\Db::i()->select( 'MAX(cfg_value)', static::$cfgTable, array('cfg_name=?', 'bot_hostport') )->first(); do{ $port = $this->checkPort( $lastport++ ); } while( !$port ); return $port; } Link to comment Share on other sites More sharing options...
TemKa_SD Posted May 1, 2019 Author Share Posted May 1, 2019 (edited) Спасибки, работает ) Два занятых порта: http://prntscr.com/njajsnУказываем первый: http://prntscr.com/njajww Результат: http://prntscr.com/njajzm Только надо добавить, чтобы поиск производился с 6112, если отсутствуют записи в БД, а если есть в БД - добавлять + 1, вдруг бот отключен и порт будет свободен, а на самом деле он уже зарезервирован. protected function checkPort($port) { $connection = @fsockopen( "212.109.217.193", $port, $errno, $errstr ); return ( $errno == "111" ) ? $port : false; } protected function getBotPort() { $lastport = \IPS\Db::i()->select( 'MAX(cfg_value)', static::$cfgTable, array('cfg_name=?', 'bot_hostport') )->first(); $lastport = $lastport ? $lastport + 1 : 6112; do{ $port = $this->checkPort( $lastport++ ); } while( !$port ); return $port; } Скажите, а зачем тут в скобках? Оно ведь и так будет работать. return ( $errno == "111" ) ? $port : false; Edited May 1, 2019 by TemKa_SD Link to comment Share on other sites More sharing options...
siv1987 Posted May 4, 2019 Share Posted May 4, 2019 Будет работать. Скобки тут для удобо-читаемости кода. Link to comment Share on other sites More sharing options...
TemKa_SD Posted May 11, 2019 Author Share Posted May 11, 2019 (edited) Здравствуйте, я пару дней назад пробовал реализовать для udp порта тоже самое, но оно почему-то не работает, и вот здесь у меня пишет udp6, может это другой протокол? http://prntscr.com/nndkgm Т.е там всегда писало что порт свободен, не было ошибок. Edited May 11, 2019 by TemKa_SD Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now