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

Create a tcp server from raw socket #220

Closed
slince opened this issue Dec 7, 2019 · 1 comment
Closed

Create a tcp server from raw socket #220

slince opened this issue Dec 7, 2019 · 1 comment
Labels

Comments

@slince
Copy link

slince commented Dec 7, 2019

Hello!

I'm trying to write a TCP application; I expect to create TCP sockets in the master process and complete bind and listen; Accept in the fork child process;

Can this library support passing raw socket to the constructor:

// create raw socket
$socket =  @\stream_socket_server($uri, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $streamContext);

// create server
$server = new TcpServer($socket, $loop);
@clue clue added the question label Dec 8, 2019
@clue
Copy link
Member

clue commented Dec 8, 2019

Hi @slince, thanks for this excellent question!

If you create a socket file descriptor and then fork the process, both the parent and child process will have access to the same socket file descriptor and can accept connections from it. This means you don't have to "create a new server" in the child process, the whole process space will be copied to the child process. That being said, forking in PHP is anything but trivial and it's way too easy to leak resources, so I wouldn't recommend doing this unless you're sure you know what you're doing.

On top of this, we're also working on a solution to create a socket server instance from an existing file descriptor number (i.e. a socket file descriptor inherited from the the parent process). This is being discussed in #164 which also includes a prototype implementation for you to give a try.

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 👍

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