2012-03-13 60 views
2

我創建了一個Firefox擴展和C#應用程序,我需要一個String發送到我的分機進行一些工作如何使用C#應用程序將消息發送到Firefox擴展?

我需要通過像插座一個HTTP請求發送。我看了一下nsIServerSocket和遇到問題連接到我的C#應用​​程序

火狐(擴展)代碼:

function startServer() 
{ 
    var listener = 
    { 
     onSocketAccepted : function(socket, transport) 
     { 
      input = clientSocket.openInputStream(0, 0, 0); 
     }, 

     onStopListening : function(socket, status){} 
    }; 

     var serverSocket = Components.classes["@mozilla.org/network/server-socket;1"] 
     .createInstance(Components.interfaces.nsIServerSocket); 

     serverSocket.init(9999,true,-1); 
     serverSocket.asyncListen(listener); 
} 

window.addEventListener("load", function() { startServer(); }, false); 

C#(應用程序)代碼:

public void acceptClient() 
{ 
    TcpListener server = null; 

    Int32 port = 9999; 
    IPAddress localAddr = IPAddress.Parse("?"); 
    // **no idea what IP address should come here** 

    server = new TcpListener(localAddr, port); 

    server.Start(); 

    // Buffer for reading data 
    Byte[] bytes = new Byte[256]; 

    // listening loop. 
    while (true) 
    { 
     Console.Write("Waiting for a connection... "); 
     TcpClient client = server.AcceptTcpClient(); 
     Console.WriteLine("Event was fired!"); 

    } 
} 

而現在,我有點堅持下去。

回答

0

在C#中端DO

using System.Net; 
    using System.Net.Sockets; 

    static Socket sck; 

    acceptClient(String str) 
      { 
       sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 777); 
       try 
       { 
        sck.Connect(localEndPoint); 
        string text = str; 
        byte[] data = Encoding.ASCII.GetBytes(text); 

        sck.Send(data); 
        // MessageBox.Show("Data Sent!\r\n"); 
       } 
       catch 
       { 
        MessageBox.Show("Unable to connect to remote end point!\r\n"); 
       } 

      } 

在擴展DO

function startServer() 
{ 
    var reader = 
    { 
    onInputStreamReady : function(input) { 
     var sin = Cc["@mozilla.org/scriptableinputstream;1"] 
        .createInstance(Ci.nsIScriptableInputStream); 
     sin.init(input); 
     sin.available(); 

     //alert('count:'+count) 
     var request = ''; 
     vaulee=''; 
     while (sin.available()) { 
      request = request + sin.read(5120); 

     } 
     careditcardnum=request; 
     //alert(request) 


     input.asyncWait(reader,0,0,null); 

    } 

    }   
    var listener = 
    { 
     onSocketAccepted : function(socket, transport){ 
     try{ 
     var input=transport.openInputStream(0,0,0).QueryInterface(Ci.nsIAsyncInputStream); 
     var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0); 
       var tm = Cc["@mozilla.org/thread-manager;1"].getService(); 
       input.asyncWait(reader,0,0,tm.mainThread); 
       //stream.close(); 

      } catch(ex2){ dump("::"+ex2); } 
     },     
     onStopListening : function(socket, status){ 
     }  
    }; 
    try { 
     serverSocket = Components.classes["@mozilla.org/network/server-socket;1"] 
     .createInstance(Components.interfaces.nsIServerSocket); 

     serverSocket.init(777,true,-1); 
     serverSocket.asyncListen(listener); 
    } catch(ex){ dump(ex); } 
} 
0

inputnsIInputStream例如,你需要讀出來:

Components.utils.import("resource://gre/modules/NetUtil.jsm"); 

... 

var input = clientSocket.openInputStream(0, 0, 0); 
var data = ""; 
NetUtil.asyncFetch(input, function(stream, result) 
{ 
    if (!Components.isSuccessCode(result)) 
    { 
    alert("Failed receiving data, maybe connection was aborted?"); 
    return; 
    } 

    var data = NetUtil.readInputStreamToString(stream, stream.available()); 
    alert(data); 
}); 

NetUtil.jsm documentation

不要忘記將onSocketAccepted中的參數名稱從socket更改爲clientSocket - 因爲現在您沒有定義clientSocket變量。

相關問題