0

我在 Java 中创建了一个套接字服务器,并正在使用 PHP 访问它。出于某种未知原因,Java 服务器正在接收奇怪的字符,例如“ÿûÿû ÿûÿû'ÿýÿûÿýnormal”,而我刚刚发送了“正常”。这是我使用的 Java 和 PHP 代码:

爪哇:

package net.david.java.xbell;

import gnu.io.SerialPort;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class NewSocket {

    public void waitForConnection(int port) throws Exception {
        ServerSocket sock = new ServerSocket(port);
        System.out.println("Server now active on port: " + port);

        Socket link = sock.accept();
        System.out.println("Interface accepted request, IP: " + link.getInetAddress());

        DataInputStream input = new DataInputStream(link.getInputStream());
        DataOutputStream output = new DataOutputStream(link.getOutputStream());


        String inputLine;

        SerialPort serial = Arduino.connect("COM3");

        OutputStream out = serial.getOutputStream();

        output.writeUTF("ISEEYOU\r\n");

        while((inputLine = input.readLine()) != null) {
            System.out.println("Server sent: \"" + inputLine + "\"");
            if(inputLine.equals("normal")) {
                System.out.println("Ringing Normal Bell...");
                out.write("8000".getBytes("UTF-8")); // TODO Ring 8 secs
                out.flush();
                Thread.sleep(1500);
                output.writeUTF("DONE\r\n");
                output.flush();
                break;
            }else if(inputLine.equals("equake")) {
                System.out.println("Ringing Earthquake Bell...");
                for(int i = 0; i < 11; i++) {
                    out.write("10000".getBytes("UTF-8")); // TODO Ring equake bell
                    out.flush();
                    Thread.sleep(1500);
                }
                output.writeUTF("DONE\r\n");
                output.flush();
                break;
            }else if(inputLine.equals("gunman")) {
                System.out.println("Ringing Gunman Bell...");
                for(int i = 0; i < 21; i++) {
                    out.write("500".getBytes("UTF-8")); // TODO Ring gunman bell
                    out.flush();
                    Thread.sleep(1500);
                }
                output.writeUTF("DONE\r\n");
                output.flush();
                break;
            }else if(inputLine.equals("sync_db")) {
                System.out.println("Syncing Database...");
                output.writeUTF("DONE\r\n");
                output.flush();
                break;
            }else{
                System.out.println("Server sent command that doesn't exist!");
                output.writeUTF("NO_EXIST\r\n");
                output.flush();
                break;
            }
        }

        input.close();
        output.close();
        serial.close();
        out.close();
        sock.close();

/*      input.close();
        link.close();
        sock.close();*/
    }
}

PHP(忽略这个,输出来自putty telnet模式):

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>XBell Control Panel - Interface</title>
        <?php
            include 'header.php';

            if(!isset($_COOKIE['usr'])) {
                header("Location: index.php");
            }
        ?>
    </head>
    <body>
        <center>
            <div id="content">
                <?php
                    if(!isset($_POST['optn'])) {
                        header("Location: ring_now.php");
                    }

                    // ROMAN - This works, don't touch.

                    //set_time_limit(0);

/*                  $client = stream_socket_client("192.168.1.102:12345");

                    if(!$client) {
                        echo 'Error connecting to the socket server! Error #1';
                        die;
                    }
                    else {
                        $str = fgets($client);
                        echo $str.'</br>';
                        if($str == "ISEEYOU") {
                            fwrite($client, "normal\r\n");
                            echo 'Sent message for normal bell!';
                            sleep(5);
                        }
                    }*/

                    $sock = fsockopen("tcp://192.168.1.102:12345");

                    if($sock) {
                        $res = fread($sock, 1024);

                        if($res == "ISEEYOU") {
                            echo "Cookie Monsta + Flux Pavilion - Come Find Me";
                        }
                    }
                ?>
            </div>
        </center>
    </body>
</html>

请注意,在 Putty 中使用 RAW 可以正常工作。

如果你能告诉我哪里出了问题,那就太好了。谢谢!

4

1 回答 1

0

使用fwrite()而不是 echo 在使用fsockopen().

于 2013-08-07T09:38:58.950 回答