簡體   English   中英

紅寶石中寫入套接字的數據無法在Java服務器中讀取

[英]data written to socket in ruby can't be read in java serversocket

我正在嘗試從開源項目BlueHydra中讀取寫入套接字的數據-> https://github.com/pwnieexpress/blue_hydra

這是將json數據寫入套接字的代碼,可在此處找到:

# write json data to result socket
TCPSocket.open('127.0.0.1', 8244) do |sock|
sock.write(json)
sock.write("\n")
sock.flush
end

現在,我有一個Java程序嘗試讀取json數據並將其寫入本地mongodb。 這是執行此操作的代碼的主要內容:

public class ReadHydraOutput {
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen    
private static int port = 8244;

public static void main(String args[]) {
    try{
        //create the socket server object
        server = new ServerSocket(port);
        //keep listening indefinitely until receives 'exit' call or program terminates
        while(true){
            System.out.println("Waiting for blue hydra client output in json");
            //creating socket and waiting for client connection
            Socket socket = server.accept();
            //read from socket to ObjectInputStream object
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            //convert ObjectInputStream object to String
            String message = (String) ois.readObject();
            System.out.println("Parsing the Json message received from blue hydra server port 8244: " + message);

            try {
                String jsonValue = "";
                HashMap<String, String> hawksMap = new HashMap<String, String>();

                JSONObject jsonObj  = new JSONObject(message);
                //parse for values
                String[] jsonNames = JSONObject.getNames(jsonObj);

                for (int i=0; i < jsonNames.length; i++) {
                    if (jsonNames[i].equalsIgnoreCase("data")) {
                        JSONObject jsonObjData  = (JSONObject)jsonObj.get(jsonNames[i]);
                        //parse again
                        String[] jsonDataNames = JSONObject.getNames(jsonObjData);
                        for (int j=0; j < jsonDataNames.length; j++) {
                            if (jsonDataNames[j].equalsIgnoreCase("le_rssi")) {
                                //nested within data field name
                                JSONObject jsonObjRssi  = (JSONObject)jsonObjData.get(jsonDataNames[j]);
                                String[] jsonrssiNames = JSONObject.getNames(jsonObjRssi);
                                for (int k=0; k < jsonrssiNames.length; k++) {
                                    jsonValue = (String) jsonObjRssi.get(jsonrssiNames[k]);
                                    hawksMap.put(jsonrssiNames[k], jsonValue);
                                }
                            } else {
                                jsonValue = (String) jsonObjData.get(jsonDataNames[j]);
                                hawksMap.put(jsonDataNames[j], jsonValue);
                            }
                        }

                    } else {
                        jsonValue = (String) jsonObj.get(jsonNames[i]);
                        hawksMap.put(jsonNames[i], jsonValue);
                    }
                }

當我運行Java程序並啟動BlueHydra時,出現以下錯誤消息:

Waiting for blue hydra client output in json
Error in ReadHydraOutput: invalid stream header: 47455420
java.io.StreamCorruptedException: invalid stream header: 47455420
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:857)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
at ReadHydraOutput.main(ReadHydraOutput.java:43)

我感覺使用ObjectInputStream的方式有些問題,但是我無法查明錯誤。

您可以使用BufferedReader從服務器套接字讀取。 只需用bufferedReader替換objectInput流。 這是示例代碼。

socketReader = new  BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = socketReader.readLine();

然后使用json庫解析該str

更改代碼:

        System.out.println("Waiting for blue hydra client output in json");
            //creating socket and waiting for client connection
            Socket socket = server.accept();
            //read from socket to ObjectInputStream object
        BufferedReader socketReader = 
        new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = socketReader.readLine();

            //ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            //convert ObjectInputStream object to String
            //String message = (String) ois.readObject();
            System.out.println("Parsing the Json message received from blue hydra server port 8244: " + message);

暫無
暫無

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

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