php 소켓 연결

php 소켓 연결

더시민 0 191 0

<?php
/*
if(isset($_GET['mode']) && $_GET['mode']) $mode =  trim(strip_tags(clean_xss_attributes($_GET['mode'])));
if(isset($_GET['com']) && $_GET['com']) $com =  trim(strip_tags(clean_xss_attributes($_GET['com'])));
if(isset($_GET['usr']) && $_GET['usr']) $usr =  trim(strip_tags(clean_xss_attributes($_GET['usr'])));
if(isset($_GET['sm']) && $_GET['sm']) $sm =  trim(strip_tags(clean_xss_attributes($_GET['sm'])));

set_time_limit(3);
if(!$mode || !$com || !$usr) exit;

include_once('./sub_site.php');
$datetime = date("Y-m-d H:i:s");

$chat_f = mysqli_fetch_array(mysqli_query($sub_site['proxy']," select * a_chat_b where computer ='{$com}' and profile = '{$usr}'  "));

*/

// Create a TCP/IP socket

$address = '1.1.1.1';
$port = 80;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Bind the socket to an address and port
socket_bind($socket, $address, $port);

// Listen for incoming connections
socket_listen($socket);

// Create an array to hold client sockets
$clients = array($socket);

while (true) {
    // Create a copy of the clients array to use with socket_select()
    $read = $clients;

    // Set up a blocking call to socket_select()
    if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL) < 1) {
        continue;
    }

    // Handle new client connections
    if (in_array($socket, $read)) {
        $newSocket = socket_accept($socket);
        $clients[] = $newSocket;

        // Send a welcome message to the client
        $message = "Welcome to the chat server!";
        socket_write($newSocket, $message, strlen($message));

        // Remove the listening socket from the read array
        $key = array_search($socket, $read);
        unset($read[$key]);
    }

    // Handle client messages
    foreach ($read as $client) {
        $data = socket_read($client, 1024);
        if ($data === false) {
            // Remove disconnected client
            $key = array_search($client, $clients);
            unset($clients[$key]);
            continue;
        }

        // Broadcast the received message to all clients
        foreach ($clients as $sendClient) {
            if ($sendClient !== $socket && $sendClient !== $client) {
                socket_write($sendClient, $data, strlen($data));
            }
        }
    }
}

// Close the sockets
foreach ($clients as $client) {
    socket_close($client);
}
socket_close($socket);

?>

<--서버쪽

 

<?php
  $address = '1.1.1.1';
  $port = 80;
  try{
    // 소켓 오브젝트를 만듭니다.
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false) {
      echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    }
    // 접속을 합니다.
    $result = socket_connect($socket, $address, $port);
    if ($result === false) {
      echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
    } 
    // 소켓 서버로부터 메시지를 받는다.
    $out = socket_read($socket, 1024);
    // 소켓 서버로 메시지를 보낸다.
    $in = "hello socket server!!";
    socket_write($socket, $in, strlen($in));
  }finally{
    socket_close($socket);
  }
?>
<!DOCTYPE html>
<html>
<head>
  <title>title</title>
</head>
<body>
  <?php echo $out?>
</body>
</html>

 

 

0 Comments
제목
Category
Facebook Twitter GooglePlus KakaoStory KakaoTalk NaverBand