PHP / Java套接字通信无法正常工作

时间:2015-06-09 12:17:04

标签: java php sockets send

我遇到以下问题已经困了两天了:我写了一个java套接字服务器,可以接收和发送数据到'localhost:64005'托管的套接字。我可以通过PHP连接到它并发送或接收消息。但是我不能发送然后收到答案。我已经将问题追溯到我写过的PHP脚本。

<?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();}
    }

}

只要我在php中调用sendSocket和getResponse方法,页面就会无限加载。但是,如果我只是调用sendSocket或getResponse(在更改java套接字之后它不会等待输入)方法,它们可以正常工作。

我做错了什么?

亲切的问候。

1 个答案:

答案 0 :(得分:1)

请参阅:http://php.net/manual/en/function.socket-recv.php

MSG_WAITALL标志将阻塞,直到它收到缓冲区的全长。您指定为2048字节的数据。

相关问题