簡體   English   中英

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

[英]PHP/Java socket communication not working

我遇到了以下問題已經被困了兩天:我寫了一個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套接字服務器:

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套接字之后它不會等待輸入)方法,它們可以正常工作。

我究竟做錯了什么?

親切的問候。

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

MSG_WAITALL標志將阻塞,直到它收到緩沖區的全長。 您指定為2048字節的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM