duanhan3067 2019-03-15 17:20
浏览 149

socket_write()无法写入套接字10053

I am trying to create a socket server that can handle several client connections, the server code is the following:

public function startWebServer()
{

    error_reporting(E_ALL);

    /* Permitir al script esperar para conexiones. */
    set_time_limit(0);


    $address = '127.0.0.1';
    $port = 25003;

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    if ($socket === false) {
        echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "
";
    }

    socket_bind($socket, $address, $port) or die ("Error al vincular socket con IP de cliente)
" . socket_strerror(socket_last_error($sock)) . "
");


    socket_listen($socket) or die ("Error al vincular socket con IP de cliente) SOCKET LISTEN 
" . socket_strerror(socket_last_error($sock)) . "
");;


    $clients = array($socket);

    do 
    {
        // create a copy, so $clients doesn't get modified by socket_select()
        $read = $clients;
        $write = null;
        $except = null;

        // get a list of all the clients that have data to be read from
        // if there are no clients with data, go to next iteration
        if (socket_select($read, $write, $except, 0) < 1)
            continue;

         // check if there is a client trying to connect
        if (in_array($socket, $read))
        {
            $clients[] = $newsock = socket_accept($socket);

            socket_write($newsock, "There are ".(count($clients) - 1)." client(s) connected to the server
");

            socket_getpeername($newsock, $ip, $port);

            echo "New client connected: {$ip}
";
            $key = array_search($socket, $read);

            // remove the listening socket from the clients-with-data array
            unset($read[$key]);
        }

         // loop through all the clients that have data to read from         

        foreach ($read as $read_sock)
        {
            // read until newline or 1024 bytes

            // socket_read while show errors when the client is disconnected, so silence the error messages
            $data = @socket_read($read_sock, 4096, PHP_BINARY_READ);

            // check if the client is disconnected
            if ($data === false)
            {
                // remove client for $clients array
                $key = array_search($read_sock, $clients);
                unset($clients[$key]);
                echo "client disconnected.
";
                continue;
            }

            $data = trim($data);

            if (!empty($data))
            {
                echo " send {$data}
";
                // do sth..
                // send some message to listening socket
                socket_write($read_sock, $data);

                // send this to all the clients in the $clients array (except the first one, which is a listening socket)
                foreach ($clients as $send_sock)
                {
                    if ($send_sock == $socket)
                        continue;

                    socket_write($send_sock, $data);

                } // end of broadcast foreach
            }
        } // end of reading foreach



    } 
    while (true);

    socket_close($socket);
}

In another test I did, managing only one client, everything turned out fine:

 while(true)
{
    $client[++$i] = socket_accept($socket);
    $message = socket_read($client[$i],1024);
    echo $message;
    $message = "Hola" . $message . "
";
    socket_write($client[$i], $i . " " . $message . "
", 1024);
    socket_close($client[$i]);
}

In the first one when I try to write to my clients I keep getting the error [10053], only when sending, not when recieveing data from my clients.

Failure to write to clients

As you can see the failure is after the echo. On the socket_write($read_sock, $data); AND socket_write($send_sock, $data);

I have heard it could be a firewall related issue, so I uninstalled my antivirus and dropped the firewall completely, even though the tests are being made locally.

I tried two tests with the client, one closing the socket connection after writing, then reading, and another without the socket_close statement.

No dice, same error on both.

What could I be doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
    • ¥15 画两个图 python或R