addr = $addr; $this->port = $port; } public function current() { if (!isset($this->cur)) $this->next(); return $this->cur; } public function rewind() { $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1); socket_bind($this->sock, $this->addr, $this->port); socket_listen($this->sock); } public function key() { return $this->count; } public function next() { $this->count++; $cur = socket_accept($this->sock); $peeraddr = ''; $peerport = ''; socket_getpeername($cur, $peeraddr, $peerport); $this->cur = array($cur, sprintf('%s[%d]', $peeraddr, $peerport)); return $this->cur; } public function valid() { return true; } } foreach (new ReceiveConnection("127.0.0.1", 9000) as $c) { list($c, $a) = $c; print "Got connection from $a\n"; $msg = "Hello World\n"; socket_send($c, $msg, strlen($msg), 0); socket_close($c); }