2013-01-02 7 views
9

के बीच सरल क्लाइंट सर्वर संचार मुझे PHP सर्वर से जावा सर्वर पर जानकारी भेजने की आवश्यकता है, लेकिन सर्वर पक्ष पर कोई भी प्राप्त नहीं कर रहा है हालांकि सर्वर पर एक प्रिंट स्टेटमेंट सफलतापूर्वक निष्पादित किया गया है, क्लाइंट का टेक्स्ट प्राप्त करने में सक्षम नहीं है सर्वर की ओर। यहाँ कोड हैं:जावा और PHP

जावा सर्वर:

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..."); 
    } 
} 

पीएचपी ग्राहक: अपने संदेश के लिए एक लाइन के अंत में जोड़ने के लिए

#!/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); 
?> 

उत्तर

0

प्रयास करें।

$message = 'Apple\n'; 

readLine हमेशा लाइन-एंड के लिए प्रतीक्षा करेगा।

+0

आप तक धन्यवाद $message = "Apple\n"; जोड़ने की जरूरत है, लेकिन एक ही समस्या ... – highlander141

संबंधित मुद्दे