PHP how to connect with sockets and read the response?

10,243

Solution 1

As suggested by others; avoid using port 22. I recommend using an obscure (unused) socket port number > 1024 such as 4444. Anything below 1024 normally requires root access. If you need to test connectivity for 22 have your server script run additional functions.

As for sending a response back to the connected client use stream_socket_recvfrom($socket, $length, 0, $peer) instead of fread()

Then on the client side add a response listener:

client.php

$socket = stream_socket_client('tcp://127.0.0.1:4444');
if ($socket) {
    $sent = stream_socket_sendto($socket, 'message');
    if ($sent > 0) {
        $server_response = fread($socket, 4096);
        echo $server_response;
    }
} else {
    echo 'Unable to connect to server';
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);

server.php

$conn = stream_socket_server('tcp://127.0.0.1:4444');
while ($socket = stream_socket_accept($conn)) {
    $pkt = stream_socket_recvfrom($socket, 1500, 0, $peer);
    if (false === empty($pkt)) {
        stream_socket_sendto($socket, 'Received pkt ' . $pkt, 0, $peer);
    }
    fclose($socket);
    usleep(10000); //100ms delay
}
stream_socket_shutdown($conn, \STREAM_SHUT_RDWR);

Run server.php which will listen in an endless loop listening for a non-empty packet once server.php receives a packet it will respond back to the connected client with the received packet.

Then execute client.php which will send 'message' to server.php Once sent it will then retrieve and echo the response from server.php which should read 'Received pkt message'

Solution 2

From http://php.net/stream_socket_accept

Accept a connection on a socket previously created by stream_socket_server().

That means it waits that one client wants to connect. (You just bind yourself to the port, but don't connect anything)

And fread is also the wrong function to use with socket_* functions. Correct function would be stream_socket_recvfrom().


But this really isn't what you seem to want. You appearently want to open a connection to some place. So fsockopen() is the right function:

$conn = fsockopen("127.0.0.1", 22, $errno, $errstr);
if (!$conn) {
  echo "$errstr ($errno)<br />\n";
} else {
  echo fread($conn, 26);
  fclose($socket);
}
Share:
10,243

Related videos on Youtube

user2493164
Author by

user2493164

Updated on June 04, 2022

Comments

  • user2493164
    user2493164 almost 2 years

    The title explains it all...

    How can i connect to an IP using tcp protocol and read/get the response? I have searched a lot but i didnt find any solution.

    $socket = stream_socket_server("tcp://127.0.0.1:22", $errno, $errstr);
    if (!$socket) {
      echo "$errstr ($errno)<br />\n";
    } else {
      while ($conn = stream_socket_accept($socket)) {
    echo fread($conn, 26);
        fclose($conn);
      }
      fclose($socket);
    }
    

    is this code ok? Does the job? Because it seems it doesn't do the job...

    Thanks in advance

    • ChunkyBaconPlz
      ChunkyBaconPlz over 10 years
      You're creating a TCP server on localhost's SSH port? What exactly are you trying to do?
    • user2493164
      user2493164 over 10 years
      I am connecting to a server, which listens to a port and then i need to get the response from the server.
    • ChunkyBaconPlz
      ChunkyBaconPlz over 10 years
      You might want to take a look at the documentation for socket_stream_server; you're creating a server, not connecting to one. Also, port 22 is a special port for SSH, I'd avoid using it if at all possible. php.net/manual/en/function.stream-socket-server.php