C# native host with Chrome Native Messaging - c#

I spent a few hours today researching how to get Chrome native messaging working with a C# native host. Conceptually it was quite simple, but there were a few snags that I resolved with help (in part) from these other questions:
Native Messaging Chrome
Native messaging from chrome extension to native host written in C#
Very Slow to pass "large" amount of data from Chrome Extension to Host (written in C#)
My solution is posted below.

Assuming the manifest is set up properly, here is a complete example for talking to a C# host using the "port" method:
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace NativeMessagingHost
{
class Program
{
public static void Main(string[] args)
{
JObject data;
while ((data = Read()) != null)
{
var processed = ProcessMessage(data);
Write(processed);
if (processed == "exit")
{
return;
}
}
}
public static string ProcessMessage(JObject data)
{
var message = data["text"].Value<string>();
switch (message)
{
case "test":
return "testing!";
case "exit":
return "exit";
default:
return "echo: " + message;
}
}
public static JObject Read()
{
var stdin = Console.OpenStandardInput();
var length = 0;
var lengthBytes = new byte[4];
stdin.Read(lengthBytes, 0, 4);
length = BitConverter.ToInt32(lengthBytes, 0);
var buffer = new char[length];
using (var reader = new StreamReader(stdin))
{
while (reader.Peek() >= 0)
{
reader.Read(buffer, 0, buffer.Length);
}
}
return (JObject)JsonConvert.DeserializeObject<JObject>(new string(buffer));
}
public static void Write(JToken data)
{
var json = new JObject();
json["data"] = data;
var bytes = System.Text.Encoding.UTF8.GetBytes(json.ToString(Formatting.None));
var stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
stdout.Write(bytes, 0, bytes.Length);
stdout.Flush();
}
}
}
If you don't need to actively communicate with the host, using runtime.sendNativeMessage will work fine. To prevent the host from hanging, simply remove the while loop and do Read/Write once.
To test this, I used the example project provided by Google here: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging
Note: I'm using Json.NET to simplify the json serialization/de-serialization process.
I hope this is helpful to somebody!

You can also use regular http communication. And send messages using fetch or other js api. And host app will be regular web api project that runs on localhost. You also will need to enable cors policy in your web app. Maybe it's overkill for some cases and probably not as fast. But it is more transparent for most developers and works great in my project.

Related

Using C# and Netduino to log data

I am developing a telemetry platform for a nano-printing project. My team is using a Netduino 2 plus (not my first choice, but what are you going to do?) I am not at all familiar with C# and am a novice programmer, to be certain.
We have some code written that successfully polls an I2C temperature sensor and uses debug.print to write to the console. I would like this data written to a file, instead.
There are examples out there to transfer files from the SD card to a PC, but this seems unnecessary to me (though it may be completely necessary in order to not overrun the buffer?). Is there a call that will simply write the data to a file instead of writing to the console?
It is my understanding that we may need an application to listen to the serial port on the PC. It seems we would also need a corresponding application to write from the hardware. I've used microcontrollers in the past that simply open a serial and send over USB to a file location.
Here is the code we are using to print the data to the console:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
namespace NetduinoApplication1
{
public class Program
{
public static void Main()
{
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
OutputPort p = new OutputPort(Pins.GPIO_PIN_SDA, true);
p.Write(false);
p.Dispose();
// write your code here
byte[] Addr = new byte[1];
Addr[0] = 0x07;
byte[] TxBuff = new byte[9];
byte[] RxBuff = new byte[9];
I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x5A, 100);
I2CDevice I2C1 = new I2CDevice(I2C_Configuration);
I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateWriteTransaction(TxBuff) };
I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateReadTransaction(RxBuff) };
while (true)
{
int iWriteCount = I2C1.Execute(WriteTran, 1000);
//Debug.Print("Write Count: " + iWriteCount.ToString());
led.Write(true);
Thread.Sleep(200);
int iReadCount = I2C1.Execute(ReadTran, 1000);
if (iReadCount >= 2)
{
int iVal = RxBuff[1] * 256 + RxBuff[0];
double Temperature = ((iVal * 0.02) - 273.15) * 9.0 / 5.0 + 32.0;
Debug.Print("Temperature: " + Temperature.ToString() + " deg F");
}
led.Write(false);
Thread.Sleep(200);
}
}
}
}
If I need to create an application, I'd like to do it, but I could really use some direction. A little experience here would go a long way.
So, a couple things come to mind.
If your Netduino has network connectivity, you could write data to a file on the SD card and then transfer the file out via FTP. There are plenty of libraries to do this with.
Same as 1, except push the data to a web service. There is an HTTPRequest library you could use to do up a quick JSON post to a web service. You would need to write the web service. But there are also plenty of free services out there that will take data feeds and store them like ubidots.
If you really want to go the serial port route, the bad news is that the Netduino's USB Serial port is reserved for debugging. But you can get a cheap little USB UART adapter to do this. There is a great blog post here that explains it.
Good Luck!

How to create a simple local web page using C# windows forms

I am looking to create a simple webpage using C# Windows Forms Application, or a C# Console application.
Running the application will begin hosting a web page at:
http://localhost:3070/somepage
I have read a little bit on MSDN about using endpoints, however being self-taught, this isn't making a ton of sense to me...
In short, this program, when running will display some text on a webpage at localhost:3070.
Sorry for such a vague question, however my hour(s) of searching for a decent tutorial haven't yielded any understandable results...
Thanks for your time!
🛑 2020 Update:
Original answer at the bottom.
Kestrel and Katana are now a thing and I would strongly recommend you look into those things as well as OWIN
Original Answer:
You will want to look into creating an HttpListener, you can add prefixes to the listener such as Listener.Prefixes.Add("http://+:3070/") which will bind it to the port your wanting.
A simple console app: Counting the requests made
using System;
using System.Net;
using System.Text;
namespace TestServer
{
class ServerMain
{
// To enable this so that it can be run in a non-administrator account:
// Open an Administrator command prompt.
// netsh http add urlacl http://+:8008/ user=Everyone listen=true
const string Prefix = "http://+:3070/";
static HttpListener Listener = null;
static int RequestNumber = 0;
static readonly DateTime StartupDate = DateTime.UtcNow;
static void Main(string[] args)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("HttpListener is not supported on this platform.");
return;
}
using (Listener = new HttpListener())
{
Listener.Prefixes.Add(Prefix);
Listener.Start();
// Begin waiting for requests.
Listener.BeginGetContext(GetContextCallback, null);
Console.WriteLine("Listening. Press Enter to stop.");
Console.ReadLine();
Listener.Stop();
}
}
static void GetContextCallback(IAsyncResult ar)
{
int req = ++RequestNumber;
// Get the context
var context = Listener.EndGetContext(ar);
// listen for the next request
Listener.BeginGetContext(GetContextCallback, null);
// get the request
var NowTime = DateTime.UtcNow;
Console.WriteLine("{0}: {1}", NowTime.ToString("R"), context.Request.RawUrl);
var responseString = string.Format("<html><body>Your request, \"{0}\", was received at {1}.<br/>It is request #{2:N0} since {3}.",
context.Request.RawUrl, NowTime.ToString("R"), req, StartupDate.ToString("R"));
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
// and send it
var response = context.Response;
response.ContentType = "text/html";
response.ContentLength64 = buffer.Length;
response.StatusCode = 200;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
}
}
}
And for extra credit, try adding it to the services on your computer!
Microsoft Relased an Open Source Project called OWIN it is simlar to Node but bottom line it allows you to host web applications in a console application:
You can find more information here:
https://github.com/duovia/duovia-http
http://owin.org/
http://katanaproject.codeplex.com/
But if you insist in creating your personal listener you can find some help here:
http://msdn.microsoft.com/en-us/library/system.net.httplistener(VS.80).aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/b7f476d1-3147-4b18-ba5e-0b3ce8f8a918/want-to-make-a-webserver-with-httplistener

Multiple Download/Upload parallely in FTP using C#

I want to do Multiple Download/Upload parallely in FTP using C# without using FTPWebRequest.
I have written my custom code and when i try to download two files simultaneously first one get download properly while second one shows size as 0 KB(it also downloads).
public void sendCommand(String command, params string[] strfilename)
{
if (command == "RETR ") //Downloading file from Server
{
FileStream output = null;
if (!File.Exists(strfilename[0]))
output = File.Create(strfilename[0]);
else
output = new FileStream(strfilename[0] , FileMode.Open);
command = "RETR " + strfilename[0];
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
Socket csocket = createDataSocket();
DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
while (timeout > DateTime.Now)
{
this.bytes = csocket.Receive(buffer, buffer.Length, 0);
output.Write(this.buffer, 0, this.bytes);
if (this.bytes <= 0)
{
break;
}
}
// this.BinaryMode = true;
output.Close();
if (csocket.Connected) csocket.Close();
this.readResponse();
MessageBox.Show("File Downloaded successfully");
else if....so on
}
}
In my main method i do like this:
ftpcommand.sendCommand("RETR ","RMSViewer.xml"); //Downloading from Server
ftpcommand.sendCommand("RETR ","cms.xml");//Downloading from Server
Any code snippet....
As Dave said, you'd need separate instances of your ftpCommand class. Look into use the BackgroundWorker to run the commands in the background (asynchronously).
How are you issuing your requests simultaneously?
Threaded?
If so - you may want to ensure you're creating separate instances of your "ftpcommand" class.
I think we'll need more information to be able to help you :)

get mac address from default gateway?

Is there a way to resolve the mac address off the default gateway using c#?
update im working with
var x = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses;
but I feel like I'm missing something.
Something like this should work for you, although you probably want to add more error checking:
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);
public static byte[] GetMacAddress(IPAddress address)
{
byte[] mac = new byte[6];
uint len = (uint)mac.Length;
byte[] addressBytes = address.GetAddressBytes();
uint dest = ((uint)addressBytes[3] << 24)
+ ((uint)addressBytes[2] << 16)
+ ((uint)addressBytes[1] << 8)
+ ((uint)addressBytes[0]);
if (SendARP(dest, 0, mac, ref len) != 0)
{
throw new Exception("The ARP request failed.");
}
return mac;
}
What you really want is to perform an Adress Resolution Protocol (ARP) request.
There are good and not soo good ways to do this.
Use an existing method in the .NET framework (though I doubt it exists)
Write your own ARP request method (probably more work than you're looking for)
Use a managed library (if exists)
Use an unmanaged library (such as iphlpapi.dll as suggested by Kevin)
If you know you only need remote to get the MAC adress of a remote Windows computer within your network you may use Windows Management Instrumentation (WMI)
WMI example:
using System;
using System.Management;
namespace WMIGetMacAdr
{
class Program
{
static void Main(string[] args)
{
ManagementScope scope = new ManagementScope(#"\\localhost"); // TODO: remote computer (Windows WMI enabled computers only!)
//scope.Options = new ConnectionOptions() { Username = ... // use this to log on to another windows computer using a different l/p
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject obj in searcher.Get())
{
string macadr = obj["MACAddress"] as string;
string[] ips = obj["IPAddress"] as string[];
if (ips != null)
{
foreach (var ip in ips)
{
Console.WriteLine("IP address {0} has MAC address {1}", ip, macadr );
}
}
}
}
}
}
You'll probably need to use P/Invoke and some native Win API functions.
Have a look at this tutorial.

C# Telnet Library

Is there a good, free telnet library available for C# (not ASP .NET)? I have found a few on google, but they all have one issue or another (don't support login/password, don't support a scripted mode).
I am assuming that MS still has not included a telnet library as part of .NET v3.5 as I couldn't find it if it was. I would loooooove to be wrong though.
Best C# Telnet Lib I've found is called Minimalistic Telnet. Very easy to understand, use and modify. It works great for the Cisco routers I need to configure.
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
Here is my code that is finally working
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
class TelnetTest
{
static void Main(string[] args)
{
TelnetTest tt = new TelnetTest();
tt.tcpClient = new TcpClient("myserver", 23);
tt.ns = tt.tcpClient.GetStream();
tt.connectHost("admin", "admin");
tt.sendCommand();
tt.tcpClient.Close();
}
public void connectHost(string user, string passwd) {
bool i = true;
while (i)
{
Console.WriteLine("Connecting.....");
Byte[] output = new Byte[1024];
String responseoutput = String.Empty;
Byte[] cmd = System.Text.Encoding.ASCII.GetBytes("\n");
ns.Write(cmd, 0, cmd.Length);
Thread.Sleep(1000);
Int32 bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.WriteLine("Responseoutput: " + responseoutput);
Regex objToMatch = new Regex("login:");
if (objToMatch.IsMatch(responseoutput)) {
cmd = System.Text.Encoding.ASCII.GetBytes(user + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(1000);
bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write(responseoutput);
objToMatch = new Regex("Password");
if (objToMatch.IsMatch(responseoutput))
{
cmd = System.Text.Encoding.ASCII.GetBytes(passwd + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(1000);
bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write("Responseoutput: " + responseoutput);
objToMatch = new Regex("#");
if (objToMatch.IsMatch(responseoutput))
{
i = false;
}
}
Console.WriteLine("Just works");
}
}
Another one with a different concept: http://www.klausbasan.de/misc/telnet/index.html
I ended up finding MinimalistTelnet and adapted it to my uses. I ended up needing to be able to heavily modify the code due to the unique** device that I am attempting to attach to.
** Unique in this instance can be validly interpreted as brain-dead.
I am currently evaluating two .NET (v2.0) C# Telnet libraries that may be of interest:
http://www.thoughtproject.com/Libraries/Telnet/
http://dotnettelnet.sourceforge.net/
Hope this helps.
Regards,
Andy.
Another one, it is an older project but shares the complete source code: http://telnetcsharp.codeplex.com/
I doubt very much a telnet library will ever be part of the .Net BCL, although you do have almost full socket support so it wouldnt be too hard to emulate a telnet client, Telnet in its general implementation is a legacy and dying technology that where exists generally sits behind a nice new modern facade. In terms of Unix/Linux variants you'll find that out the box its SSH and enabling telnet is generally considered poor practice.
You could check out:
http://granados.sourceforge.net/ - SSH Library for .Net
http://www.tamirgal.com/home/dev.aspx?Item=SharpSsh
You'll still need to put in place your own wrapper to handle events for feeding in input in a scripted manner.

Categories