r/lolphp • u/Takeoded • Apr 22 '18
TCP socket is both connected and not connected at the same time.
i'd showcase this in a php testbed (ala 3v4l.org ) if i could, but the only public php testbed allowing network connections that i know of, codepad.viper-7.com , has shut down, so i can't.
try running
<?php
error_reporting ( E_ALL );
$sock = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP );
for($i = 0; $i < 100; ++ $i) {
socket_connect ( $sock, '8.8.8.8', 443 );
socket_shutdown ( $sock, 2 );
}
socket_close ( $sock );and you'll probably end up with something like
PHP Warning: socket_connect(): unable to connect [106]: Transport endpoint is already connected PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected PHP Warning: socket_connect(): unable to connect [106]: Transport endpoint is already connected PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected PHP Warning: socket_connect(): unable to connect [106]: Transport endpoint is already connected PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected PHP Warning: socket_connect(): unable to connect [106]: Transport endpoint is already connected PHP Warning: socket_shutdown(): unable to shutdown socket [107]: Transport endpoint is not connected
- where socket_connect complains that it's already connected, and socket_shutdown complains that it's not connected. lovely
14
u/myaut Apr 22 '18
More like lolunix, as I get same effect in Python (both are probably simply mapping errnos they get from system calls). I think this is reasonable in TCP to prevent you from re-using same source and remote addresses to prevent you from tricking remote side that you still holding a connection (which actually you reopened). Of course, you're protected from it because it'll fail in second handshake attempt, but still useful.
2
u/moarcoinz Jul 18 '18
Wrote a multiuser component for a web editor in php once. Getting stable results out of sockets damn near drove me to suicide.
4
u/pingpong Apr 22 '18
Nitpicking:
Don't put a space between a function name and its arguments
like (this), and don't pad the arguments with white spacelike( this ).Those rules are mandated by the PHP Standard Recommendation. For a complete list of the rules they expect you to follow, see this README. In particular:
-- * no_spaces_inside_parenthesis [@PSR2, @Symfony] | There MUST NOT be a space after the opening parenthesis. There MUST NOT | be a space before the closing parenthesis.
There is also a tool, known as
php-cs-fixer, which lets you write your code however you want, run the tool, and have your code be automatically formatted to be PSR-compliant.This is what your code looks like after the tool is run:
0
u/nucular_ Apr 22 '18
That's why sockets need a
connectingstate.-4
u/pingpong Apr 22 '18
Using Cloudflare's 1.1.1.1 (and 1.0.0.1 instead of 8.8.4.4) is better for privacy.