我正在创建一个Firefox附加组件,其中包含服务器侦听一个端口上的TCP连接。问题是它并不总是关闭连接:在发送FIN-ACK并接收到ACK之后,如果客户端没有发送返回FIN-ACK,则TCP会话保持打开状态。
只有部分连接未完全关闭。但过了一段时间,他们的TC连接太多了,Firefox无法打开新的文件句柄,或者接收到任何新的连接。
TCP localhost.localdomain:commtact-https->localhost.localdomain:46951 (CLOSE_WAIT)
我找不到方法,最好是在附加组件中(但我也尝试在客户端)确保所有TCP连接都正确关闭。以下是加载项中服务器功能的外观:
init_server: function(port) {
server.result = {};
server.listener = {
onSocketAccepted : function(serverSocket, transport) {
server.result.sout = transport.openOutputStream(0,0,0);
var instream = transport.openInputStream(0,0,0);
server.result.sin = Components.classes["@mozilla.org/binaryinputstream;1"].
createInstance(Components.interfaces.nsIBinaryInputStream);
server.result.sin.setInputStream(instream);
server.debug("New incoming connection");
var dataListener = {
onStartRequest: function(request, context) {
server.debug("onStartRequest");
},
onStopRequest: function(request, context, status) {
server.debug("onStopRequest");
// instream.close();
// delete instream;
// server.result.sout.close();
// server.result.sin.close();
},
onDataAvailable: function(request, context, inputStream, offset, count) {
var sis = Components.classes ["@mozilla.org/scriptableinputstream;1"].createInstance (Components.interfaces.nsIScriptableInputStream);
sis.init(inputStream);
var str = sis.read(count);
var suc = Components.classes ["@mozilla.org/intlscriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
suc.charset = "utf-8";
var line = suc. ConvertToUnicode (str);
server.debug(line);
[ ... process data ... ]
// instream.close();
// server.result.sout.close();
// server.result.sin.close();
// delete instream;
},
};
// Listen to new data
var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
pump.init(instream, -1, -1, 0, 0, false); //screenserver.result.sin
pump.asyncRead(dataListener, null);
},
onStopListening : function(serverSocket, status){
server.debug("Client dead?");
server.init_server(server.PORT);
},
};
server.socket = null;
server.socket = Components.classes["@mozilla.org/network/server-socket;1"].getService(Components.interfaces.nsIServerSocket);
server.socket.init(port, true, -1);
server.socket.asyncListen(server.listener);
}
答案 0 :(得分:2)
我认为您需要保存在调用onSocketAccepted
中传递的传输,然后在其上调用close()
。