通过firefox tcp socket.send()发送的数据不能返回直到关闭套接字 [英] data send by firefox tcp socket.send() can't retrive until close the socket
问题描述
client.java
数据成功发送到 server.java
如何使用这个firefox脚本发送数据,它实际上连接到服务器。 send(text);
不能实时工作.i意思是当我关闭套接字时,服务器显示收到的数据 socket.close();
。但是我知道 server.java
代码没有问题。 这不起作用
setTimeout(function(){socket.send(i'm firefox);},5000); //因为套接字尚未关闭
这项工作
setTimeout(function(){socket.send(i'm firefox);},5000);
setTimeout(function(){socket.close();},6000);
但我真的不想关闭套接字,因为我想发送大量数据一个。
$ b $ p这里是在scratchpad测试完整的代码[-browser]
var tcpSocket = Cc [@ mozilla.org/tcp-socket;1\"].createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open(127.0.0.1,5000);
$ b setTimeout(function(){socket.send(i'm firefox);},5000);
//setTimeout(function(){socket.close();},8000);//我是firefox文本,当运行这个行/关闭套接字时,由服务器检索。
我认为java代码并不重要,但
它实际上工作。您正在收到您的数据,但您目前正在等待新的一行打印收到的消息: = in.readLine())!= null){
System.out.println(Server:+ inputLine);
out.println(inputLine);
if(inputLine.equals(Bye。)){
break;
$ b $ p
$ b当前只有在关闭套接字时才会发生。只要在消息的最后添加一行,它就可以按预期工作:
var tcpSocket = Cc [@ mozilla .ORG / TCP套接字; 1\" ]的createInstance(Ci.nsIDOMTCPSocket);
var socket = tcpSocket.open(127.0.0.1,5000);
函数sendMessage(msg){
socket.send(msg +\\\
);
setTimeout(function(){
sendMessage(hi mark);
},3000);
setTimeout(function(){
sendMessage(hi johnny);
},5000);
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 java class which is client.java
data successfully sent to server.java
how ever when i use this firefox script to send data ,it actually connect to the server .but send("text");
doesn't work realtime .i mean sever shows received data when i close the socket socket.close();
. but i know there is no problem with server.java
code.
this doesn't work
setTimeout(function(){socket.send("i'm firefox");},5000); // because socket isn't closed yet
this work
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 lot of data one by one.
here is the complete code tested on scratchpad [-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 isn't important.but here it is.
I'm asking why do i need to close the socket to send data ? and how can i send data without close the socket ?
update
i made a Gif to show my problem here you can see data not sending real time but when socket is closed all the data flushed .
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);
这篇关于通过firefox tcp socket.send()发送的数据不能返回直到关闭套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!