写入ruby中的socket的数据无法在java serversocket中读取

时间:2018-01-29 00:02:18

标签: java ruby mongodb sockets

我正在尝试从开源项目BlueHydra中读取写入套接字的数据 - > https://github.com/pwnieexpress/blue_hydra

以下是将json数据写入套接字的代码,找到here

$('#selectWithSomeID').change(function(){
    val directDisplayString = "",
        directDisplayExtras = "",
        directDisplayPrice = "";
    if ($(this).val() === "450 Litre"){
        directDisplayString = '450 Litre Direct Storage Vessel with:';
        directDisplayExtras = '50 Litre Expansion Vessel'+'<br/>'+'1" Combination Valve (3 Bar PRV / 6 Bar SRV)'+'<br/>'+'3/4" T&P Valve 90°C 7 Bar, 25 kW Discharge Rate'+'<br/>'+'Tundish';
        directDisplayPrice = "ÂŁ2,750";
    }
    else if ($(this).val() === "somethingelse")
    ...
    $("#directChoiceDisplay").text(directDisplayString);
    $("#directChoiceExtras").text(directDisplayExtras);
    $("#directChoicePrice").text(directDisplayPrice);
});

现在,我有一个java程序试图读取该json数据并将其写入本地mongodb。以下是执行此操作的代码:

# 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程序并启动BlueHydra时,我收到以下错误消息:

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);
                    }
                }

我觉得有些东西与我使用ObjectInputStream的方式有关,但我无法查明错误。

1 个答案:

答案 0 :(得分:0)

您可以使用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);