2016-07-19 8 views
0

私は、Javaプログラムと通信するWebサイトを作成しようとしています。私はどのフォームのソケットにも新しく、Javaプログラムで自分のWebサイトに返信するのに問題があります。私は私のプログラムを起動し、私はページだけで継続的に何も印刷せずにロードする私のWebページを読み込むとJavaとPHPのソケット通信の問題

<?php 
function send($message) { 
    $address = 'localhost'; 
    $port = 4400; 
    $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); 
    try { 
     socket_connect($socket, $address, $port); 
     $status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port); 
     if($status != false) { 
      // If it worked then wait for a response? 
      // This is where the problem is at 
      if($next = socket_read($socket, $port)) { 
       echo $next; 
      } 
      return true; 
     } 
     return false; 
    } catch(Exception $e) { 
     return false; 
    } 
} 
if(send("requeststatus")) { 
    echo "Worked"; 
} 
?> 

while(true) { 
    ServerSocket socket = null; 
    Socket connection = null; 
    InputStreamReader inputStream = null; 
    BufferedReader input = null; 
    DataOutputStream response = null; 
    System.out.println("Server running"); 
    try { 
     socket = new ServerSocket(4400); 
     while(true) { 
      connection = socket.accept(); 
      inputStream = new InputStreamReader(connection.getInputStream()); 
      input = new BufferedReader(inputStream); 
      String command = input.readLine(); 
      System.out.println("command: " + command); 
      if(command.equals("restart")) { 
       break; 
      } else if(command.equals("requeststatus")) { 
       String reply = "testing"; 
       System.out.println(reply); 
       response = new DataOutputStream(connection.getOutputStream()); 
       response.writeUTF(reply); 
       response.flush(); 
      } 
     } 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if(socket != null) { 
      try { 
       socket.close(); 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(connection != null) { 
      try { 
       connection.close(); 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(inputStream != null) { 
      try { 
       inputStream.close(); 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(input != null) { 
      try { 
       input.close(); 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(response != null) { 
      try { 
       response.close(); 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    System.out.println("Server Closing"); 
} 

そして、私の現在のPHPコード:ここに私の現在のJavaコードです。私は、PHPがすべてを実行し、スクリプトが終了し、返事を待っているときにスクリプトが "ブロックされて"いる後、ブラウザに結果を表示すると思いますか?もしそうなら、どうすれば私のJavaプログラムに "requeststatus"を送り、私のJavaプログラムに応答させるPHPスクリプトを得ることができますか?最終目標は、Javaプログラムからの応答を自分のWebサイトに表示することです。

また、私はこのシステムを非効率的かつ間違って書いていると確信しています。このタイプのシステムを書く正しい方法は何ですか?任意のヒント?読んでくれてありがとう。

答えて

1
String command = input.readLine(); 

これは、改行またはストリームの終了を待ちます。改行を送信することは決してありません。ストリームを閉じると出力に書き込むことはできません。

基本的にメッセージに改行を追加します。