2013-08-07 39 views
0

我已经创建了Java中的套接字服务器,并且正在使用PHP访问它。由于某种未知的原因,Java服务器正在接收奇怪的字符,比如我刚刚发送“正常”的“ÿûÿûÿÿÿÿÿÿÿÿnormal”。下面是我使用Java和PHP代码:Java Socket服务器接收怪异字符

的Java:

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(忽略这一点,输出腻子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> 

注意,该工程完美在腻子中使用RAW很好。

如果你能告诉我什么是错的,那就太好了。 谢谢!

+0

我没有看到PHP代码中的任何地方,你实际上*发送*任何东西到'$ sock'?或者它是我们应该看的评论过的代码? –

+0

糟糕,输出来自Putty的telnet模式。我蠢蠢欲动。 – cheese5505

回答

0

使用fwrite()而不是回写在用fsockopen()创建的套接字上。

+0

试了很多次,失败了很多次。 – cheese5505

+0

在PHP脚本中,我忘了发送任何东西到套接字,但是我会这样做。谢谢。 – cheese5505