简体   繁体   English

在Java服务器和PHP客户端之间进行通信

[英]Communicate between Java server and PHP client

I was trying to create the socket communication between the Java server and the PHP client. 我试图在Java服务器和PHP客户端之间创建套接字通信。 The only thing I got is the connection, but strange message( messy code) received from PHP client. 我唯一得到的是连接,但是从PHP客户端收到了奇怪的消息(混乱的代码)。 Here is the code about the Java server: 以下是有关Java服务器的代码:

public class TestServer extends Thread {
private ServerSocket serverSocket;

public TestServer(int port) throws IOException {
    serverSocket = new ServerSocket(port);
}

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort()
                    + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            InputStreamReader inputStream = new InputStreamReader(server.getInputStream());
            BufferedReader input = new BufferedReader(inputStream);
            DataOutputStream response = new DataOutputStream(server.getOutputStream());

            String command = input.readLine();
            System.out.println(command);
            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

public static void main(String[] args) {
    int port = Integer.parseInt(args[0]);
    try {
        Thread t = new TestServer(port);
        t.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

} }

And the code in the PHP client: 以及PHP客户端中的代码:

 <?php


            $ip="192.168.1.6";     //Set the TCP IP Address to connect too
            $port="9999";        //Set the TCP PORT to connect too



                //Connect to Server
                $socket = stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);

                    if($socket) {
                        //Start SSL
                        stream_set_blocking ($socket, true);
                        stream_socket_enable_crypto ($socket, true, STREAM_CRYPTO_METHOD_SSLv3_CLIENT);
                        stream_set_blocking ($socket, false);

  $command = array("autospray", "on");
  $sock= $socket;
  $msg = $command;

function write(&$sock,$msg) {
    //$msg = "$msg\n\0";
    $length = strlen($msg);
    while(true) {
        $sent = @socket_write($sock,$msg."\n" ,$length)  or die("Could not write output\n");

@socket_shutdown($sock, 2);
@socket_close($sock);


        if($sent === false) {
            return false;
        }
        if($sent < $length) {
            $msg = substr($msg, $sent);
            $length -= $sent;
            print("Message truncated: Resending: $msg");
        } else {
            return true;
        }
    }
    return false;
}


}




                            ?> 

In PHP you encrypt the socket with: stream_socket_enable_crypto(), so the JAVA server will receive an encrypted stream in inputStream. 在PHP中,您使用以下代码对套接字进行加密:stream_socket_enable_crypto(),因此JAVA服务器将在inputStream中接收加密的流。 You need to decrypt the stream to get the real message. 您需要解密流以获取真实消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM