swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.25k stars 3.16k forks source link

Coroutine\Socket客户端绑定地址失败 #5301

Closed menzhu closed 3 weeks ago

menzhu commented 3 weeks ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.
    
    <?php

use Swoole\Coroutine; use function Swoole\Coroutine\run;

run(function () { $socket = new Coroutine\Socket(AF_INET, SOCK_RAW, 1); $res = $socket->bind('4.4.4.12'); var_dump($res); var_dump($socket->errCode); $retval = $socket->connect('4.4.4.10', 0, 1); while ($retval) { $n = $socket->send('hello'); var_dump($n);

    $data = $socket->recv();
    var_dump($data);

    //发生错误或对端关闭连接,本端也需要关闭
    if ($data === '' || $data === false) {
        echo "errCode: {$socket->errCode}\n";
        $socket->close();
        break;
    }

    Coroutine::sleep(1.0);
}

var_dump($retval, $socket->errCode, $socket->errMsg);

});



2. What did you expect to see?
绑定源IP成功

3. What did you see instead?
绑定源IP失败

4. What version of Swoole are you using (show your `php --ri swoole`)?
swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.1.2
Built => Feb 18 2024 14:31:15
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1f  31 Mar 2020
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

5. What is your machine environment used (show your `uname -a` & `php -v` & `gcc -v`) ?
Linux ubuntu-virtual-machine 5.13.0-39-generic #44~20.04.1-Ubuntu SMP Thu Mar 24 16:43:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

PHP 8.2.16 (cli) (built: Feb 18 2024 13:33:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.16, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.16, Copyright (c), by Zend Technologies

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
NathanFreeman commented 3 weeks ago

贴一下errCode属性的值,看一下错误原因

NathanFreeman commented 3 weeks ago
<?php
use Swoole\Coroutine;
use function Swoole\Coroutine\run;

run(function () {
    $socket = new Coroutine\Socket(AF_INET, SOCK_STREAM, SOL_TCP);
    var_dump($socket->errCode);
    $retval = $socket->connect('127.0.0.1', 9501, 1);
    while ($retval)
    {
        $n = $socket->send('hello');
        var_dump($n);

        $data = $socket->recv();
        var_dump($data);

        //发生错误或对端关闭连接,本端也需要关闭
        if ($data === '' || $data === false) {
            echo "errCode: {$socket->errCode}\n";
            $socket->close();
            break;
        }

        Coroutine::sleep(1.0);
    }

    var_dump($retval, $socket->errCode, $socket->errMsg);
});

你要连接tcp服务端的话尝试这么写,swoole关于协程socket客户端的文档我要改一下

menzhu commented 3 weeks ago

贴一下errCode属性的值,看一下错误原因

errCode是22,Invalid argument。我是网卡上配置了多IP,想循环使用不同的IP发送ICMP请求。我用php原生的socket_bind是可以成功的

matyhtf commented 3 weeks ago

目前对于 SOCKET_RAW 的支持并不完整,ICMP 的话建议直接用 php 的函数

menzhu commented 3 weeks ago

目前对于 SOCKET_RAW 的支持并不完整,ICMP 的话建议直接用 php 的函数

👌