我用Java创建了一个套接字服务器,并使用PHP访问它。由于某些未知原因,Java服务器接收到奇怪的字符,例如“ÿûÿûÿûÿû'ÿýÿûÿýnormal”,我刚发送“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非常合适。
如果你能告诉我什么是错的,那就太好了。 谢谢!