Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write never send data #273

Closed
mastir opened this issue Oct 27, 2021 · 2 comments
Closed

Write never send data #273

mastir opened this issue Oct 27, 2021 · 2 comments
Labels

Comments

@mastir
Copy link

mastir commented Oct 27, 2021

When i test my server, i see new connection, but no data sent.

$minimal = hex2bin('78780d0103587350716231840064b1450d0a').hex2bin('78780a1305060400020067e3370d0a').hex2bin('78781f12120612102129c60533baf803bf642414544b00ff0675330005e9001941d00d0a');

$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);
$port = $argv[1] ?? 33664;
$connector->connect('127.0.0.1:'.$port)->then(function ($conn) use ($loop) {
    $conn->on('data', function($chunk){
        echo '.';
    });
    $conn->on('error', function(){
        echo 'error';
    });
    $conn->on('close', function(){
        echo 'close';
    });
    echo 'Send'.PHP_EOL;
    $conn->send($minimal);
    $conn->end();

});

$loop->run();
echo 'done?'.PHP_EOL;

iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

I started nc -l 0.0.0.0 33664 and nothing. I see "Send" message, but nothing ever happens. Only if i stop nc i see "close" message. But when i send a big amount of data it works. Tried piping 2mb file with test data and it worked so I tried to save into file this data and pipe it - nothig comes.

@mastir
Copy link
Author

mastir commented Oct 27, 2021

Tried without react and everything works

$port = $argv[1] ?? 33664;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, '127.0.0.1', $port);
echo 'Connect: '.($result ? 'OK' : 'FAIL');
if (!$result)exit;

$minimal = hex2bin('78780d0103587350716231840064b1450d0a').hex2bin('78780a1305060400020067e3370d0a').hex2bin('78781f12120612102129c60533baf803bf642414544b00ff0675330005e9001941d00d0a');

echo 'Got:';
socket_write($socket, $minimal, strlen($minimal));
while ($data = socket_read($socket, 2048)) {
    echo chunk_split(bin2hex($data),2,' ');
}
echo "close..\n\n";
socket_close($socket);

Connect: OKGot:78 78 05 01 00 64 ed 77 0d 0a 78 78 05 13 00 67 ef c1 0d 0a

@mastir mastir changed the title Write is never sending data Write never send data Oct 27, 2021
@clue clue assigned clue and unassigned clue Nov 29, 2021
@clue clue added the question label Nov 29, 2021
@clue
Copy link
Member

clue commented Nov 29, 2021

@mastir Thanks for reporting, this is a nasty one!

It looks like there's s small oversight and all you're missing is a use ($minimal) in your code. Because this is missing, you're essentially passing an undefined variable to $conn->send() which PHP will report (as a warning in PHP 8+ and as a notice in earlier versions) and continue with a null value, effectively sending nothing.

Your IDE of choice or static analysis tools such as phpstan should be able to point you to this variable. On top of this, you can configure PHP to show this warning/notice to you during runtime.

If you're using an error handler that turns this warning/notice into an Exception or Throwable, the code will currently cause the promise to be rejected on this line. We're working on reactphp/promise#87 to improve default error reporting in this case.

I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍

@clue clue closed this as completed Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants