【问题标题】:Sending message from a Java socket to a PHP server socket将消息从 Java 套接字发送到 PHP 服务器套接字
【发布时间】:2015-06-21 23:26:34
【问题描述】:

我有一个监听 PHP 中某个端口的套接字,我试图使用 Java 中的套接字发送一个字符串,但我不断收到以下错误:

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 套接字类如下所示:

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

我做错了什么?

【问题讨论】:

标签: java php sockets serversocket


【解决方案1】:

我认为问题在于您试图从 PHP 端的服务器套接字读取,而不是客户端套接字:

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

这应该是

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

【讨论】:

  • 啊,你是对的。没关系,我没有意识到我在重写代码时做出了改变。
【解决方案2】:

我发现了问题。

有两个问题,我从错误的套接字读取,所以我按照@RealSkeptic 的建议进行了更改,更改为:

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

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

另一个问题是我使用的内部 while 循环是对 socket_read 进行全局覆盖。

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

但由于通信的流程,它在这里产生了一个问题:

答案:

在 Java 端,我只发送一个响应,而在 PHP 端,我在 while 循环中“无限地”使用 socket_read,直到收到字符串“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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-24
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多