Skip to content

Instantly share code, notes, and snippets.

@polonskiy
Last active August 29, 2015 14:07
Show Gist options
  • Save polonskiy/0e1a289a6cf55a89ecdc to your computer and use it in GitHub Desktop.
Save polonskiy/0e1a289a6cf55a89ecdc to your computer and use it in GitHub Desktop.
echo servers in php and go
<?php
$server = stream_socket_server('tcp://127.0.0.1:8000');
while (true) {
$client = stream_socket_accept($server, -1);
if (pcntl_fork() === 0) {
stream_copy_to_stream($client, $client);
die;
}
fclose($client);
while (pcntl_wait($status, WNOHANG));
}
package main
import (
"io"
"net"
)
func main() {
server, _ := net.Listen("tcp", "127.0.0.1:8000")
for {
client, _ := server.Accept()
go func() {
io.Copy(client, client)
client.Close()
}()
}
}
package main
import (
"io"
"net"
)
func main() {
server, _ := net.Listen("tcp", "127.0.0.1:8000")
for {
client, _ := server.Accept()
io.Copy(client, client)
client.Close()
}
}
<?php
$server = stream_socket_server('tcp://127.0.0.1:8000');
while (true) {
$client = stream_socket_accept($server, -1);
stream_copy_to_stream($client, $client);
fclose($client);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment