1

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???

cwishva
  • 1,940
  • 1
  • 18
  • 23
  • Welcome to Stack Overflow. Please read [How to Ask](http://stackoverflow.com/questions/how-to-ask), [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/), and [How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html). –  Mar 05 '12 at 13:15

3 Answers3

2

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!\r\n");
            }
            catch
            {
                MessageBox.Show("Unable to connect to remote end point!\r\n");
            }

        }

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); }
}
cwishva
  • 1,940
  • 1
  • 18
  • 23
0

It's easiest to use TCP sockets. Firefox add-ons can run TCP servers using nsIServerSocket. You can see a pretty simple server implementation here (belongs to the Extension Auto-Installer extension). This isn't an HTTP server - it merely throws away whatever it considers HTTP headers without looking at them. There is also a full HTTP server implementation that's being used for Firefox unit tests - but you probably don't want it that complicated.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • @cwishva: Stack Overflow is not the place where somebody will write code for you. If you tried something and are unsure why it doesn't work - feel free to ask here. – Wladimir Palant Mar 11 '12 at 12:17
  • ya i do understand Thanks for the help i got the connection but having trouble with reading the string thats send from the C# application onSocketAccepted : function(socket, transport){ alert("came") var input =transport.openInputStream(0, 0, 0); alert(input) } the string i send should be in the var input rite?? – cwishva Mar 13 '12 at 10:30
  • @cwishva: No, `input` is a stream that you need to read. You can use [NetUtil.readInputStreamToString()](https://developer.mozilla.org/en/JavaScript_code_modules/NetUtil.jsm#readInputStreamToString%28%29) for example. – Wladimir Palant Mar 13 '12 at 10:40
  • input.available() give me 0 which means there are no string received but when i send it from the app the connection will be established any reason? – cwishva Mar 13 '12 at 10:58
0

You could try SignalR.

https://github.com/SignalR/SignalR

MattW
  • 12,902
  • 5
  • 38
  • 65