5 Replies - 1437 Views - Last Post: 04 June 2011 - 04:40 AM Rate Topic: -----

#1 Deevee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-February 09

Asynchronous data interaction: C# and the web (http/php etc.)

Posted 02 June 2011 - 01:30 AM

Hello! I was wondering if I could get a little advice as to 'best practice' with reference to a problem I am working on at the moment. I can think of several ways of actually doing this but I don't have any experience of the practical ins and outs or pitfalls of any particular method.

My scenario thus:
I have a webserver sitting on the internet. It is a typical Linux-apache-php-mysql box and is very happy to host webpages. Nothing too exciting.

I also have a PC which, in a different physical location. This machine is running Windows and is primed with .net so it can run c# applications. It is connected to the internet and is running dyn-dns so we always know how to reach it logically even if its location changes.

The PC is connected to a number of sensors. The sensors are simple enough to control but are not the sort which stream data continuously. A request is made, they take a reading and report back some time later (a couple of seconds perhaps) .

The PC has a c# app running on it. You can click a button, it returns a sensor value at that point in time.

So far so good.

I want to be able to replicate this interface to the senors remotely. If I was doing it on another PC somewhere on the network it would be a simple matter of setting up a Sockets or TCP type connection and bouncing some packets around.

It would be better to replicate the interface on a website. Users click a link or press a button and after a short delay a response is returned with a new sensor value.

Once a link has been set up between the C# on my sensor PC and the website its easy to do things like plotting data with pretty Jquery graphs and/or storing readings in an MySQL database for review later.

I know that PHP can generate tcp-socket type connections but I am also aware that C# has a web services component which as far as I can tell allows for interactions via http GET/POST.

What are the pros/cons of each of these methods? Is there another way to do this I have overlooked? Is one method better for this type of interaction, if so why? (I know UDP is not suitable for this, for example). How easy is it to deal with requests from multiple users/sessions? (if its a web page more than one person might be asking for data at once). Is anything special going to be needed on the web side of things once the request has been made, to get the data back again?

I've got my C# books open on the table and I have had a dig around online in general for setting up this type of client-server model but I still can't seem to see a clear way through.

Your thoughts and comments would be most appreciated.

tl;dr - I want to control a C# application from a webpage and send data in both directions once a connection is esablished.

Thanks in advance
D

Is This A Good Question/Topic? 0
  • +

Replies To: Asynchronous data interaction: C# and the web (http/php etc.)

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4314
  • View blog
  • Posts: 7,481
  • Joined: 08-June 10

Re: Asynchronous data interaction: C# and the web (http/php etc.)

Posted 02 June 2011 - 06:52 AM

Well, I do have one question first. Is there a reason that it has to update itself live? What about setting up a service that, every minute, polls the sensors and writes the data to a database (or sends it to the PHP site for it to write to a DB)?

If that'll work for you, then I'd suggest making a Windows Service that uses timers and performs WebRequests.

If it must be live, and only live, then I'd suggest making a web service. Now, I've never consumed a SOAP service with PHP or anything other than another C# application, so I don't know how hard it is.

If you were planning on using jQuery's ajax call to GET the data, here's what I've done before. I've created an ASP.NET MVC 3 application, and made a specific controller that returned a JsonResult. So in that way, you can just $.get a URL, and it'll come back with JSON directly to your javascript.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Asynchronous data interaction: C# and the web (http/php etc.)

Posted 02 June 2011 - 07:15 AM

Consuming a SOAP service with PHP is pretty easy, as long as you've got a WSDL from which to work.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4314
  • View blog
  • Posts: 7,481
  • Joined: 08-June 10

Re: Asynchronous data interaction: C# and the web (http/php etc.)

Posted 02 June 2011 - 07:23 AM

I still like the MVC3 idea better, if he's going to be working with jQuery graphs. Heck, you can do that (with a bit more work) with regular ASP.NET, by using Response.Write to output the JSON.
Was This Post Helpful? 0
  • +
  • -

#5 Deevee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-February 09

Re: Asynchronous data interaction: C# and the web (http/php etc.)

Posted 04 June 2011 - 03:56 AM

Sorry for the delay in getting back to this, I have been away from my desk a lot this week.

Thanks for your responses, you guys are touching on some things which I am not overly familiar with so apologies up front is these seem like silly questions - just need to ensure that I have understood things correctly.

Curtis Rutland - Having the system generate a new sensor reading at interval 'n' is ultimately quite wasteful in this instance. I'm not interested in a time dependant flow of data values, its more like a monitoring thing to check what is happening at a remote location (oven left on, doors open, how dry is the soil in my plant pot).

For now all I need is a webpage with a link on it. Click the link - request is sent via the internets to the remote PC. C# does its thing and returns a value to the server. Now whether it writes the result directly to the DB and then sends an alert telling the web-session that it should check the DB or as you suggested throws the value straight back in to the web side of things (from which DB things can be done and graphics generated) I'm not sure at this point.

I've never dealt with ASP.net nor MVC. Surely though, this requires my web hosting to support ASP.net etc. Our hosting solution for this is LAMP so that is not an option. Unless you meant the ASP MVC thing was running on the client PC and then there is a webserver-to-webserver type interaction going on. Sorry if I sound thick on this point.

JackOfAllTrades - I know in principal what a WSDL is but where would I get one for PHP. Is it something I would have to implement myself?

I need to go away I think and have a read around these topics to get a better understanding, thanks for the pointers. I will - invariably - have more questions.

Thanks again!
D
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Asynchronous data interaction: C# and the web (http/php etc.)

Posted 04 June 2011 - 04:40 AM

Quote

JackOfAllTrades - I know in principal what a WSDL is but where would I get one for PHP. Is it something I would have to implement myself?

If you're making the service to which you're connecting, then you create the WSDL yourself. If you're consuming someone else's web service, they should provide the WSDL. Of course, if you're using WCF (Windows Communication Foundation), you can write your code in C# and .NET will do all the service generation stuff for you.

Quote

I need to go away I think and have a read around these topics to get a better understanding, thanks for the pointers. I will - invariably - have more questions.

That is the right course of action. Web services is a pretty broad subject.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1