2013-01-02 11 views
9

Ho bisogno di inviare informazioni dal client php al server java, ma nessuno sta ricevendo sul lato server nonostante un'istruzione di stampa sia stata eseguita correttamente sul server, il testo dal client non è in grado di ricevere su il lato server. Ecco i codici:comunicazione client server semplice tra java e php

Java Server:

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 Cliente:

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

risposta

8

Got it!,

abbiamo bisogno di aggiungere $message = "Apple\n"; invece di $message = 'Apple\n';

0

tenta di aggiungere un fine linea per il vostro messaggio.

$message = 'Apple\n'; 

readLine attenderà sempre la fine linea.

+0

Grazie Fino, ma lo stesso problema ... – highlander141

Problemi correlati