2013-01-02 9 views
9

PHPクライアントからJavaサーバーへ情報を送信する必要がありますが、サーバー側で1つのprintステートメントがサーバー上で正常に実行されますが、クライアントからのテキストは受信できませんサーバー側。ここで、コードは次のとおりです。javaとphp間の単純なクライアントサーバー通信

のJavaサーバー:

import java.io.BufferedReader; 
import java.net.*; 
import java.io.*; 

public class javaphp2 { 
    private static ServerSocket socket; 

    private static Socket connection; 
    private static String command  = new String(); 
    private static String responseStr = new String(); 

    private static int port = 4309; 

    public static void main(String args[]) { 
     System.out.println("Signal Server is running."); 

     try { 
      socket = new ServerSocket(port); 

      while (true) { 
       connection = socket.accept(); 

       InputStreamReader inputStream = new InputStreamReader(connection.getInputStream()); 
       DataOutputStream response = new DataOutputStream(connection.getOutputStream()); 
       BufferedReader input = new BufferedReader(inputStream); 

       command = input.readLine(); 
       //System.out.println("The input is" + command); 
       response.writeBytes(responseStr); 
       response.flush(); 
       //response.close(); 

       System.out.println("Running"); 
      } 
     } catch (IOException e) { 
      System.out.println("Fail!: " + e.toString()); 
     } 

     System.out.println("Closing..."); 
    } 
} 

PHPクライアント:

#!/usr/local/bin/php -q 
<?php 
$address = '132.119.90.165'; 
$port = 4309; 

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); 
socket_connect($socket, $address, $port); 

$message = 'Apple'; 
$len = strlen($message); 

$status = socket_sendto($socket, $message, $len, 0, $address, $port); 
if($status !== FALSE) 
{ 
    $message = ''; 
    $next = ''; 
    while ($next = socket_read($socket, 4096)) 
    { 
     $message .= $next; 
    } 

    echo $message; 
} 
else 
{ 
    echo "Failed"; 
} 

socket_close($socket); 
?> 

答えて

8

がそれを手に入れた!、私たちが代わりに$message = 'Apple\n';

0

は、あなたのメッセージにラインエンドを追加してください。

$message = 'Apple\n'; 

readLineは常に行末を待機します。

+0

$message = "Apple\n";を追加する必要が

はティルありがとうございますが、同じ問題... – highlander141

関連する問題