Firefox tcp socket.send () data transfer cannot be retrieved until socket is closed

I am trying to send some data from Firefox to a java desktop application. So my java class works as a server, and the Firefox script works as a client. When I test it using another java class that is client.javadata successfully sent to server.java, when ever, when I use this firefox script to send data, it actually connects to the .but server does send("text");not work in real time. I mean sever shows the received data when I close the socket socket.close();. but I know server.javathere is no problem with the code .

this does not work

setTimeout(function(){socket.send("i'm firefox");},5000); // because socket isn't closed yet

this job

setTimeout(function(){socket.send("i'm firefox");},5000);
setTimeout(function(){socket.close();},6000); 

but I really don't want to close the socket because I want to send a lot of data one by one.

here is the full code tested on notepad [-browser]

var tcpSocket = Cc["@mozilla.org/tcp-socket;1"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open("127.0.0.1", 5000);

setTimeout(function(){socket.send("i'm firefox");},5000);
//setTimeout(function(){socket.close();},8000);// i'm firefox text retrieve by server when run this line / when close the socket.

I think java code doesn't matter. but here it is.

I ask, why do I need to close the socket to send data? and how can I send data without closing the socket?


update

I made a Gif to show my problem here, you can see data not sending in real time, but when the socket is closed, all data is reset.

enter image description here

+1
source share
1 answer

It really works. Your data will be received, but you expect 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;
    }
}

. , :

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); 
+2

All Articles