data send by firefox tcp socket.send() can't retrive until close the socket

前端 未结 1 1480
耶瑟儿~
耶瑟儿~ 2021-01-16 11:21

I\'m trying to send some data from Firefox to java desktop application .so my java class work as a server and Firefox script work as a client .when i test it using another j

1条回答
  •  不思量自难忘°
    2021-01-16 11:55

    It's actually working. Your data is being received, but you're currently waiting for a new line to print your received messages:

    while ((inputLine = in.readLine()) != null) {
        System.out.println("Server: " + inputLine);
        out.println(inputLine);
    
        if (inputLine.equals("Bye.")) {
            break;
        }
    }
    

    Which currently only happens when you close the socket. Just add a new line at the end of your messages and it will work as expected:

    var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
    var socket    = tcpSocket.open("127.0.0.1", 5000);
    
    function sendMessage(msg){
        socket.send(msg + "\n");
    }
    
    setTimeout(function(){
        sendMessage("hi mark");
    },3000);
    
    setTimeout(function(){
        sendMessage("hi johnny");
    },5000); 
    

    0 讨论(0)
提交回复
热议问题