2015-04-15 118 views
2

我有一个套接字在PHP中侦听某个端口,我试图发送一个字符串在Java中使用套接字,但我不断收到以下错误:发送消息从Java套接字到PHP服务器套接字

Warning: socket_recv(): unable to read from socket [0]: The operation completed successfully. 
in H:\Dropbox\EISTI\www\java-instagram-web\src\Client\Component\Server\Client.php on line 55 

没有更多的描述,很难理解是什么问题。

我的PHP类如下所示:

class Client { 
    private $address; 
    private $port; 
    private $command; 

    public function __construct($port, $address, $addressServer, $portServer, $command) 
    { 
     set_time_limit(0); 
     $this->address = $address; 
     $this->port = $port; 
     $this->init(); 
    } 

    private function init(){ 

     //Create socket 
     if (! $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) { 
      $this->showError('socket create'); 
     } 
     echo "Server Created\n"; 

     //Bind socket 
     if (!socket_bind($socket, $this->address, $this->port)) { 
      $this->showError('socket bind'); 
     } 
     echo "Server bind to $this->address and $this->port \n"; 

     if (!socket_listen($socket)) { 
      $this->showError('socket listen'); 
     } 
     echo "Server Listening \n"; 

     do { 
      $client = socket_accept($socket); 
      echo "connection established\n"; 

      $message = "\n Hey! welcome to the server\n"; 
      socket_write($client, $message, strlen($message)); 

      do { 
       if (! socket_recv($socket, $clientMessage, 2045, MSG_WAITALL)) { 
        $this->showError('socket receive'); 
       } 
       $message = "Command Received\n"; 
       echo $clientMessage; 

       socket_send($client, $message, strlen($message), 0); 

       if (!$clientMessage = trim($clientMessage)) { 
        continue; 
       } 

       if (trim($clientMessage) == 'close') { 
        socket_close($client); 
        echo "\n\n--------------------------------------------\n". 
         "ClientRequest terminated\n"; 
        break 1; 
       } 
      } while(true); 

     } while(true); 
    } 

    private function showError($message) { 
     echo ("Error: ".$message); 
     exit(666); 
    } 
} 

而且我的Java Socket类如下所示:

public class ResponseToClient { 
    private String host; 
    private int port; 
    private Socket socket; 
    private PrintStream theOut; 
    private String resultLocation; 

    /** 
    * Constructor 
    */ 
    public ResponseToClient(String path) { 
     this.host = "localhost"; 
     this.port = 1000; 
     this.resultLocation = path; 
    } 

    /** 
    * Setting up Socket 
    */ 
    public void init(){ 
     try{ 

      socket = new Socket(host, port); 
      theOut = new PrintStream(socket.getOutputStream()); 

      //Send Command 
      sendCommand(); 

      //Closing connections 
      socket.close(); 
      theOut.close(); 

     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Send Command 
    */ 
    public void sendCommand() 
    { 
     theOut.println(Messages.type_response + Messages.seperator_client + resultLocation); 
     System.out.println(Messages.type_response + Messages.seperator_client + resultLocation); 
    } 
} 

我在做什么错?

+0

你可能结帐这个问题有一个类似的问题。 http://stackoverflow.com/a/11610618/2863 –

回答

1

我想通解决问题。

有两个问题,我是从错误的插槽读书,所以我做的改变由@RealSkeptic的建议,更改:

socket_recv($socket, $clientMessage, 2045, MSG_WAITALL) 

socket_recv($client, $clientMessage, 2045, MSG_WAITALL) 

而另一个问题是我正在使用的内部while循环是en_ globing socket_read。

我这样做了,因为我已经从我在PHP中找到的server_sockets教程中获得了这段代码。

但它创造了一个问题就在这里,因为通信流量:

答:

在Java方面,我只发送一个单一的反应,而在PHP端我使用socket_read“无限“在while循环中,直到我收到字符串”close“。这是造成这个问题的原因,因为在收到第一个答复之后,没有其他东西可读。因此错误。

因此,要解决这个问题,我只需要删除while循环,(我删除了socket_write作为我的目的,我不需要发送任何信息)。

该类Client工作例如:

class Client { 

    private $addressServer; 
    private $portServer; 
    private $address; 
    private $port; 
    private $command; 

    public function __construct($port, $address, $addressServer, $portServer, $command) 
    { 
     set_time_limit(0); 
     $this->addressServer = $addressServer; 
     $this->address = $address; 
     $this->portServer = $portServer; 
     $this->port = $port; 
     $this->command = $command; 
     $this->init(); 
    } 

    private function init() { 

     //Send request to the Java server 
     $request = new Request(
      $this->addressServer, $this->portServer, $this->command 
     ); 

     //create socket 
     if (! $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) { 
      $this->showError('socket create'); 
     } 
     echo "Server Created\n"; 

     //Bind socket 
     if (!socket_bind($socket, $this->address, $this->port)) { 
      $this->showError('socket bind'); 
     } 
     echo "Server bind to $this->address and $this->port \n"; 

     if (!socket_listen($socket)) { 
      $this->showError('socket listen'); 
     } 
     echo "Server Listening \n"; 

     do { 
      $client = socket_accept($socket); 
      echo "connection established\n"; 

      if(!$clientMessage = socket_read($client, 10000, PHP_NORMAL_READ)){ 
       $this->showError('socket read'); 
      } 

      echo "Command Received\n"; 
      echo $clientMessage; 

     } while(true); 
    } 

    private function showError($message){ 
     echo ("Error: ".$message); 
     exit(666); 
    } 
} 
1

我相信问题是,你正试图从你的服务器插槽上的PHP端读取,而不是你的客户端套接字:

socket_recv($socket, $clientMessage, 2045, MSG_WAITALL) 

这应该是

socket_recv($client, $clientMessage, 2045, MSG_WAITALL) 
+0

啊你是对的。没关系,我没有意识到我在重写代码的同时做出了改变。 –

相关问题