2017-11-17 2 views
0

라이브 서버 (우분투 14.04, 램프 스택 실행)가 작동 중입니다.포트가 열려 있어도 PHP TCP/IP 서버에 연결할 수 없습니까?

최근에 PHP 코드를 사용하여 TCP/IP 서버에 추가했습니다. 이것은 내가 사용한 코드입니다. 친절 IP 주소가 자리 표시 자입니다 참고 : 내 서버에 업로드 php filename.php를 실행 한 후 위의 코드는 오류를 표시하지 않습니다

<?php 
error_reporting(E_ALL); 

/* Allow the script to wait for connections. */ 
set_time_limit(0); 

/* Activate the implicit exit dump, so we'll see what we're getting 
* while messages come. */ 
ob_implicit_flush(); 

$address = '123.456.789.123'; 
$port = 1235; 

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { 
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; 
} 

if (socket_bind($sock, $address, $port) === false) { 
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; 
} 

if (socket_listen($sock, 5) === false) { 
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; 
} 

//clients array 
$clients = array(); 

do { 
    $read = array(); 
    $read[] = $sock; 

    $read = array_merge($read,$clients); 

    $write = NULL; 
    $except = NULL; 
    $tv_sec = 5; 

    // Set up a blocking call to socket_select 
    if(socket_select($read, $write, $except, $tv_sec) < 1) 
    { 
     // SocketServer::debug("Problem blocking socket_select?"); 
     echo "socket continuing"; 
     continue; 
    } 

    // Handle new Connections 
    if (in_array($sock, $read)) {   

     if (($msgsock = socket_accept($sock)) === false) { 
      echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; 
      break; 
     } 
     $clients[] = $msgsock; 
     $key = array_keys($clients, $msgsock); 

     $msg = "\Welcome to the PHP Test Server. \n" . 
     "You are the customer number: {$key[0]}\n" . 
     "To exit, type 'quit'. To close the server type 'shutdown'.\n"; 
     socket_write($msgsock, $msg, strlen($msg)); 

    } 

    // Handle Input 
    foreach ($clients as $key => $client) { // for each client   
     if (in_array($client, $read)) { 
      if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) { 
       echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($client)) . "\n"; 
       break 2; 
      } 
      if (!$buf = trim($buf)) { 
       continue; 
      } 
      if ($buf == 'quit') { 
       unset($clients[$key]); 
       socket_close($client); 
       break; 
      } 
      if ($buf == 'shutdown') { 
       socket_close($client); 
       break 2; 
      } 
      $talkback = "Client {$key}: You said '$buf'.\n"; 
      socket_write($client, $talkback, strlen($talkback)); 
      echo "$buf\n"; 
     } 

    }   
} while (true); 

socket_close($sock); 
?> 

, 그리고 그것이 동시에 여러 연결을 처리 할 수 ​​있다고 생각합니다.

<?php 

error_reporting(E_ALL); 

$fp = fsockopen("123.456.789.123", 1235, $errno, $errstr, 30); 
if (!$fp) { 
    echo "$errstr ($errno)<br />\n"; 
} else { 
    fwrite($fp, "You message"); 
    while (!feof($fp)) { 
     echo fgets($fp, 128); 
    } 
    fclose($fp); 
} 

?> 

다시, IP 주소 자리 표시 자입니다 :

지금, 소켓에 연결,이 PHP 코드를 시도했다. 나는이 프로그램을 실행할 때

그러나, 내 브라우저에서 다음과 같은 출력을 얻을 :

Connection refused (61)

그래서 내가 할 첫 번째 일은 내 서버에 로그인하는 것입니다

는 다음을 통해 포트를 허용 UFW 가능 .
. 
. 
1235      ALLOW IN Anywhere 
1235/tcp     ALLOW IN Anywhere 
1235/udp     ALLOW IN Anywhere 
. 
. 

그래서 지금 내가 소켓에 연결 내 테스트 클라이언트를 방지 무엇을 그렇게 확실하지 않다 다음 UFW 상태에서 자세한 검사를 수행 한 후, 나는 다음 있습니다.

누구나 올바른 방향으로 나를 가리킬 수 있습니까? 도움이된다면 도움이 될 것입니다. 고맙습니다.

답변

0

IP/포트 일치 및 오류 로그를 확인하십시오. 그냥 사용해보십시오.

function write(&$sock,$msg) { 
    $msg = "$msg\n\0"; 
    $length = strlen($msg); 
    while(true) { 
     $sent = socket_write($sock,$msg,$length); 
     if($sent === false) { 
      return false; 
     } 
     if($sent < $length) { 
      $msg = substr($msg, $sent); 
      $length -= $sent; 
      print("Message truncated: Resending: $msg"); 
     } else { 
      return true; 
     } 
    } 
    return false; 
} 
관련 문제