Пример #1
2
 #####################################################################
 foreach ($read_socket_list as $read_socket) {
     if ($read_socket == $main_socket) {
         ## 요청 수락 ##################################################
         $sub_socket = socket_accept($main_socket);
         socket_getpeername($sub_socket, $addr, $port);
         $addr_port = "{$addr}:{$port}";
         $socket_list[$addr_port] = $sub_socket;
         echo "<{$addr_port}>[Connect]\n";
         #############################################################
     } else {
         socket_getpeername($read_socket, $addr, $port);
         $addr_port = "{$addr}:{$port}";
         if (!isset($handshake_check[$addr_port])) {
             ## 핸드쉐이크 ##############################################
             handshake($read_socket);
             $handshake_check[$addr_port] = true;
             #########################################################
         } else {
             $bytes = socket_recv($read_socket, $buffer, 2048, 0);
             /* 바이트 값을 하나씩 출력
                for ($i = 0; $i < strlen($buffer); $i++) {
                    echo ord($buffer[$i]).',';
                }
                echo "\n";
                */
             if ($bytes === false || (ord($buffer[0]) & 15) == 8) {
                 ## 연결종료 ###########################################
                 unset($socket_list[$addr_port]);
                 unset($handshake_check[$addr_port]);
                 socket_close($read_socket);
Пример #2
0
function handshake($absnum)
{
    if ($absnum == 1) {
        return 0;
    } else {
        return $absnum - 1 + handshake($absnum - 1);
    }
}
Пример #3
0
$socket = stream_socket_server("tcp://" . $GLOBALS['socket_ip'] . ":" . $GLOBALS['socket_port'], $errno, $errstr);
if (!$socket) {
    consolemsg("Error! Cant run socket server: " . $errstr . "(" . $errno . ")");
    consoleend();
    die($errstr . "(" . $errno . ")\n");
}
$connects = array();
while (true) {
    $read = $connects;
    $read[] = $socket;
    $write = $except = null;
    if (!stream_select($read, $write, $except, null)) {
        break;
    }
    if (in_array($socket, $read)) {
        if (($connect = stream_socket_accept($socket, -1)) && ($info = handshake($connect))) {
            consolemsg("New connection; connect=" . $connect . ", info=" . $info . "; OK");
            $connects[] = $connect;
            onOpen($connect, $info);
        }
        unset($read[array_search($socket, $read)]);
    }
    foreach ($read as $connect) {
        $data = fread($connect, 100000);
        if (!$data) {
            consolemsg("Connection closed: " . $connect);
            fclose($connect);
            unset($connects[array_search($connect, $connects)]);
            onClose($connect);
            consolemsg("OK");
            continue;