0

The following code should return the whole message all the time but it doesn't... I keep refreshing and at some point it prints:

Connected to UWT-RTR-002

And some others as expected:

Connected to UWT-RTR-002 Wireshark full trace start/stop control program Type "help" for instructions 2024-07-08 12:08:50 command:

Then I tried to pass the command "help" but I get nothing... It works when I use telnet on Windows Command Prompt.

UPDATE: while is taking too long, it doesn't display any error.

$address = '10.0.0.200';
$port = 41200;
$sock = stream_socket_client("tcp://".$address.":".$port, $errno, $errstr); 
fwrite($sock, "help\n");
while ($data = fread($sock, 8192)){
    echo $data."<br>";
}
fclose($sock);

Trying to read and send commands to a TCP

5
  • There are no message boundaries in TCP. fread() can return less than you asked for, and it won't necessarily match a message that was written.
    – Barmar
    Commented Jul 8 at 16:16
  • You need to call fread() in a loop until you get the entire message.
    – Barmar
    Commented Jul 8 at 16:16
  • This is what I did now and yeah it brought the whole message but it took too long even though it is just two lines: while($data = fread($sock,9)){ echo $data."<br>"; } Commented Jul 8 at 17:38
  • That loop won't end until the other end closes the connection. You need to define an application protocol so you will know when to end the loop. E.g. you can send the message length followed by that many bytes of message text.
    – Barmar
    Commented Jul 8 at 18:53
  • @Barmar I am not really sure about what you are suggesting... would you be able to rewrite that while the way you suggested? Commented Jul 8 at 19:21

2 Answers 2

0

Since TCP doesn't have message boundaries, you have to define a way to tell how long the message is yourself. One option is to send the message length first, then read that many bytes in a loop.

This code will work as long as messages are less than 256 bytes long.

$address = '10.0.0.200';
$port = 41200;
$sock = stream_socket_client("tcp://".$address.":".$port, $errno, $errstr); 
$msg = "help";
fwrite($sock, chr(strlen($msg)));
fwrite($sock, $msg);
$length = ord(fread($sock, 1));
$recv_msg = '';
while ($length > 0 && $data = fread($sock, $length)){
    $recv_msg .= $data;
    $length -= strlen($data);
}
echo $recv_msg . "<br>";
fclose($sock);
7
  • ohh I see what you are trying to do but then it doesn't work because it doesn't display the whole thing... for example... when I run this code it returns: onnected to UWT-RTR-001 Wireshark full trace start/stop control pro Commented Jul 8 at 19:47
  • Did you fix the other end to send the length first?
    – Barmar
    Commented Jul 8 at 19:54
  • I didn't change your code, where do you expect me to change it? Commented Jul 8 at 20:17
  • There are two programs, a client and a server. This is the client, you also need to make similar changes to the server.
    – Barmar
    Commented Jul 8 at 20:22
  • Since you didn't post the server code, I can't show the changes in detail. But if you understand the general principle, it should be straightforward for you to do it.
    – Barmar
    Commented Jul 8 at 20:22
0

This is what the server-guy did and it loads fast:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

set_time_limit(0);
ob_implicit_flush();

$address = '10.0.0.200';
$port = 41200;
echo "<b>Address:</b> ".$address." <b>PORT</b> ".$port."\n";


$socket = stream_socket_client("tcp://".$address.":".$port,$errno, $errstr, 30);
if (!$socket)
{
    echo "error opening socket";
    die();
}

stream_socket_sendto($socket, "help\n");

$command_sent = 0;

while(!feof($socket))
{
    $data = stream_get_line($socket, 1500, "\n");
    echo $data."<br>\n";
    if ($data == 'command:')
    {
        if ($command_sent == 0)
        {
            stream_socket_sendto($socket, "status\n");
        }
        else
        {
            stream_socket_sendto($socket, "exit\n");
        }
        $command_sent = 1;
    }
}

fclose($socket);
echo "done\n";
?>
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 9 at 16:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.