Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

sockets - Communication from C# Application to Firefox Extension

I need to send some strings (name, number) from my C# application to my Firefox Extension and i have read some techniques of doing but no idea how to implement it.

  1. Pipes
  2. Sockets(HTTP request)

If its using HTTP Request its better.

UPDATE

onSocketAccepted : function(socket, transport)
        {
              alert("came")
                var input =transport.openInputStream(0, 0, 0);
                alert(input)
   }

does the message i send from the C# application will be in var input???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In C# Side 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!
");
            }
            catch
            {
                MessageBox.Show("Unable to connect to remote end point!
");
            }

        }

In the Extension 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); }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...