2015-06-09 32 views
4

我被困了兩天,出現以下問題:我寫了一個Java套接字服務器,它可以接收和發送數據到託管在'localhost:64005'的套接字。我可以通過php連接到它併發送或接收消息。但是我無法發送,然後收到答案。我已經將問題追溯到我編寫的php腳本。PHP/Java套接字通信不起作用

<?php 
    class socketCommunication{ 
     protected $PK; 
     protected $ip = '127.0.0.1'; 
     protected $port = 64005; 
     protected $socket; 
     public $result; 

     function __construct($key){ 
     $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 
     $this->result = socket_connect($this->socket, $this->ip, $this->port) or die("Could not connect to server\n"); 
     $this->PK = $key; 
     $this->sendSocket(); 
     } 

     function getResponse(){ 
      //$input = socket_read($this->socket, 1024) or die("Could not read"); 
      $bytes = socket_recv($this->socket, $buf, 2048, MSG_WAITALL); 
      return $buf; 
     } 

     function sendSocket(){ 
      $len = strlen($this->PK); 
      socket_send ($this->socket, $this->PK, $len, 0); 
     } 
    } 
?> 

<?php 
    //include("/mysql/RandomQuery.php"); 
    include("/java/socketCommunication.php"); 

    $object2 = new socketCommunication(100001); 
    echo $object2->getResponse(); 
?> 

java的socket服務器:

package Server; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.util.concurrent.Semaphore; 

import Database.Reader; 

public class Receive implements Runnable { 
    Semaphore semaphore; 
    private Socket connection; 
    String result = "default"; 

    public Receive(Socket conn, Semaphore sem){ 
     this.connection = conn; 
     this.semaphore = sem; 
    } 

    @Override 
    public void run() { 
      try { 
       semaphore.acquire(); 
       System.out.println(connection.toString()); 
       BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       PrintWriter out = new PrintWriter(connection.getOutputStream(), true); 
       String userInput; 
       while ((userInput = in.readLine()) != null) { 
        System.out.println(userInput); 
        Reader reader = new Reader(Integer.parseInt(userInput)); 
        this.result = reader.getResult(); 
        System.out.println(result); 
        out.println(result); 
        break; 
       } 
       connection.close(); 
       in.close(); 
       System.out.println("input is closed"); 
       semaphore.release(); 
       } catch (IOException | InterruptedException e) {e.printStackTrace();} 
    } 

} 

只要我叫兩個sendSocket和GETRESPONSE方法在PHP的頁面只是不斷加載無限。但是,如果我只是調用sendSocket或getResponse(更改java套接字以便它不會等待輸入)方法,則它們將正常工作。

我做錯了什麼?

親切的問候。

回答

1

參見:http://php.net/manual/en/function.socket-recv.php

的MSG_WAITALL標誌將阻塞,直到它接收到的緩衝器的全長。你已經指定了2048個字節的數據。

+0

看來並非如此。我目前正在使用一種解決方法,使用2個套接字。 1代表接收,1代表發送。我收到可變長度的消息。 –

+0

當您嘗試接收而不發送php腳本時會發生什麼? 當您比較兩個腳本中的緩衝區讀取方式時,您將逐漸獲取Java中的緩衝區塊,但只是將緩衝區分配給php變量。你需要在php中採用相同的方法。瀏覽器不會加載頁面,直到這些連接已解決,並且您的讀取連接看起來像保持打開狀態,因爲它正在等待接收更多數據。瀏覽器在6年前開始這樣做,而不是在讀取它們阻塞的緩衝區時將實時數據傳送到頁面。 –

+0

當我收到沒有發送時,我得到由java服務器發送的消息沒有任何問題。我所做的解決方法迄今爲止效果很好。我認爲如果我用不同的線程處理髮送和接收,實際上可能會更好。我會解決這個問題。不管怎麼說,還是要謝謝你 –