socket connection code of php

前端 未结 2 891
孤城傲影
孤城傲影 2020-12-29 17:45

I am writing a simple php socket code.
Here is my code

 

        
2条回答
  •  渐次进展
    2020-12-29 18:04

    That is the expected behaviour of waiting. The program you have written is a socket server which is ready to listen to the connection with the specified port, until then it will wait.

    You can create a client who connects so that you will see the response "Client is here". The client can be any programming language including PHP.

    Below is a sample code in PHP (I didn't verify it).

    $fp = stream_socket_client("127.0.0.1:9875", $errno, $errstr);
    
    if (!$fp) {
        echo "$errstr ($errno)
    \n"; } else { // Handle code here for reading/writing }

    You can check this link for sample client code in PHP.

    EDIT

    $host = "127.0.0.1";
    $port = 9875;
    $timeout = 30;
    $sk = fsockopen($host, $port, $errnum, $errstr, $timeout);
    if (!is_resource($sk)) {
        exit("connection fail: " . $errnum . " " . $errstr);
    } else {
        echo "Connected";
    }
    

提交回复
热议问题