Which programming language should I use for TCP server for JavaScript clients? - c#

I am asking for advise on which programming language is most acceptable for the following situation.
The program will act as the server in a TCP networking application, serving JavaScript clients that output to the browser, using a pre-written framework.
The server program will need to be 'always-on', and be capable of dealing with JSON.
My first instinct is to use PHP, because it can run the same web-server, and has pre-existing JSON and TCP functions. Is there a way to run PHP scripts on the server without needing to have a browser open to 'trigger' the script execution? - the script will have to be running for hours on end without timing out.
Other languages that are considered are C#, C++, Java.
Thanks in advance.

node.js
The first example on the homepage shows how easy it is to build servers in it
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

Anything can make a request that triggers a PHP script to be executed, it doesn't have to be a browser. For example, a cron job can simply request a particular page and it will be executed. Or it can execute it directly.

It looks like you are trying to run a php script to push data to the browser in real time. PHP is not ideal for this since it is io-blocking and will become terribly slow/crash when there are not enough available threads. This makes it very bad from a scalability standpoint. Java is ideal for this since it gives you the ability to control how threads are handled. If you are indeed looking to push data to your browser in real-time, xhr long polling is what you want. APE-server is by far the best solution I have found for this:
http://www.ape-project.org/
As a side note, you can run php scripts serverside without a browser. For a linux system, you will need php-cli to do this. To get php-cli, in terminal, type in sudo -s, hit enter, type your password, hit enter, type sudo apt-get install php-cli, hit y, then hit enter again. Then, create a file called yourfilename.run in the same directory as your php file and insert the following into the .run file:
php -f name_of_php_file.php
Allow the file to be executed in terminal (by right clicking it and selecting it), then double click it to open it in terminal. Voila, your script is running without a browser.
But once again, if you are trying to push data to your browser in real-time, php is a bad choice. Take the time to look into ape-server.

Related

Can I use a php file on a server, to send data to a C# file which then validates the data, and sends the result back to the php file?

I'll try to be as simplistic and transparent as I can.
My goal is to: send leaderboard data to a server, have it validated, then added to a MySQL table..
How I want to achieve this:
Player completes game, gets highscore data.
The highscore data is sent to a file on the server, "leaderboard.php"
The leaderboard.php file sends the playthrough data to a C# file on the server, "PlaythroughSimulator.cs" to run a simulation of the playthrough
After the PlaythroughSimulator.cs file has confirmed or denied the validity of the playthrough, it sends a true/false value back to the leaderboard.php file
If the recording was valid, the leaderboard.php file adds it to the table as expected.
I have the php process working for receiving and adding scores to the leaderboards on the server, and I've created the playthrough simulator in C# OUTSIDE of the server.
My problem now is bringing the playthrough simulator onto the server and making it work entwined with the php script.
I did a bit of reading and see that I should be able to call C# scripts from php, but I'm not sure how to send or retrieve data between them.
Generally speaking, different programs on a computer don't know or care what language each other are written in; they care what protocols they speak - what inputs they expect, what outputs they'll give, and where that information needs to be passed.
For instance, when you have data in the browser and you want to send it to a PHP program, the browser doesn't need to know what PHP is; instead, it sends the data over the internet, using the HTTP protocol. On the server, a process listens for that request, and it just happens that you've set that up so that it runs your PHP script.
In the same way, you won't be telling PHP to "run a C# file"; you'll be telling it to send some data to another program, and that program will happen to be written in C#. An important consequence of this is that it helps you break the problem down, always a key skill in programming, because you can run the program you wrote in C# directly, and confirm how it behaves, before telling PHP to talk to it.
The simplest "protocol" to use in this scenario is a command-line script:
Write a C# program that takes the playthrough data as a command-line argument, or reads it from a file, and outputs a result
Use shell_exec in your PHP code to run the script, passing it the data, or the name of a temporary file containing the data

What fiddler do? Why can it fix my application through open and close?

It is not a specific problem because I have more than one experience that fiddler save my app.
A recent example:
I had an excel addin app write in C#. In the app I had a httpclient to connect to my server through ssl. And I ignored the certification in my code explicitly.
The app always worked well(I mean the connection part), but one day in a QA environment, it failed in no causes. The connection seemed to be blocked for long long time.
Coincidentally, we opened the Fiddler and tried to grab some http. Then everything worked fine.
(Fiddler may do something in the middle)
Then after closing Fiddler, we cannot recreate the problem because all things went back to fine again.
(Fiddler change something permanently!)
Here are some clues:
We use the normal C# http clients System.Net.Http .
Our server is ssl (https, but we must ignore the cert)
The app was good in the same environment at frist.
The lastest change is about the localization, and the test enviroment changed the windows local and language.
We are under a company network, using a proxy in the example above.
The failure is not the whole connection module, but a specific request.(In our case, a request that post two small xml files including excel itself)
Please give us some hint and guess, so we can feel better on this mystical problem.
How long is a "long long time" exactly?
Fiddler does not make any persistent changes to the system; if you start and stop it, the system is returned to the prior configuration. I suppose it's remotely possible that if some configuration setting were corrupt before Fiddler ran, that corruption wouldn't be replicated after Fiddler attempted to reapply it, but we don't know of any such "corrections."
This article describes some of the things folks find unexpected in terms of Fiddler "magically" fixing things while it runs.

C# Program working with PHP server

Just curious about the best way to go about this. I want to make a webpage that shows the amount of time it takes to ping all the machines on my network. I want a C# program to do the pinging and report it to the webpage. I want to be able to click "Re-ping" on the webpage and the program rerun the test. What is the best way to go about this?
The biggest issue is how to get things from the user to the C# program.
I can make the program post on the server, and the server show the user, like below.
User <----> PHP Host <---- C# Program
However how could I get data to go the other way?
PHP Host ---?-->C# Program
Am I thinking about this all wrong?
I do want the pinging and other things to be handled by a C# program as it has far more complex functionality (threading, connections, etc) than a PHP host that is interpreted as it is requested.
Is it possible that I could let the C# program return pages to users and avoid a PHP server all-togeather?
Any ideas are welcomed.
You have many options. If you want to avoid using a PHP server the simplest would probably be to write a simple http server similar to this. Other options include using ASP.NET with a C# backend, or a webservice, which would be accessed from the PHP script or directly by a client application.

C# Web Server for displaying Console Output?

I have just finished writing my c# console application, and I am contemplating embedding a web server into it (probably this one http://webserver.codeplex.com). I don't do much in the way over advanced web coding though, so I am not sure if I can do what I need to.
Basically, I would like to allow users to view the console output of my application in realtime just by visiting the site being served by my application. If I understand correctly, to do something like this it would require AJAX, which a simple C# Web Server wouldn't be able to handle.
Is this correct or is there an easy way to do this I am missing?
How to re-route console output
You will need to write your own TextWriter and make Console use it via Console.SetOut. This writer should notify connected web clients, as well as the original Console.Out.
How to host a COMET-like server
You can use HttpListener and some basic async programming to do this. If you wrap the HttpListenerContext.Response.OutputStream in a StreamWriter (with AutoFlush set to true) and set HttpListenerContext.Response.SendChunked to true clients will receive partial results - this means you can even do it in an IFRAME.
You will need to add rights to the URL for yourself if UAC is enabled:
netsh http add urlacl url=http://+:9090/ user=domain\username
Code?
I couldn't resist it; I have written a (poorly tested and mostly incomplete) sample.
That is incorrect.
AJAX is a client side technique relying on JavaScript. As long as the web server can respond to an HTTP request, it can server AJAX content (whatever that might mean:-).
I would use this UtilDev Cassini as me embedded web server of choice, it is based on the code from the embedded Visual Studio dev server and can run pretty much anything that runs in IIS. AJAX is a browser technology not a server technology, the server just sees http requests the same as any other. My last point would that it seems slightly odd to embed a web server in a console application. It would be more usual to do this with a windows service. Have you considered converting the console app into a windows service?

Communicating and Controlling web page from C#

I have already opened a webpage in Firefox which is actually running through the apache web server on the same machine. And I have C# application running on the same machine.
Now I want to refresh/inform my already open web page that C# has finished processing.
I have seen the threads all over the web and also some similar threads on Stackoverflow but did not find solution.
Any idea how to solve this?
In details: C# Application is transferring images from external storage. Web page (http://127.0.0.1/mypage.php) is looking for the images periodically, but never know that all images are transferred or not so that it can process these images to do some more work.
In this scenario, we want to tell our web page (which is already running in firefox) all data transferred and now you can refresh to process data.
Two steps: Let the server communicate that C# is done, and have the web page react to it
You somehow need to expose the fact that the C# program has finished to the web. If you also have an IIS running, this could be done via a URL served by IIS directly that returns a value indicating whether C# is still running or not. If you don't, you can write to a file, database or whatever and have a script on your Apache server that checks for this value. Whatever you choose, you want something like www.myserver.com/are_you_finished.[php, aspx, whatever] that returns 1 or 0
Then, you can build JavaScript script in your client page that periodically checks this URL and reacts as soon as the value is 1. This would be a typical AJAX call, ie you need to play with XmlHttpRequest. This could be elaborated much further, but maybe you first say if that's what you have in mind, and I also think there's a lot of good documentation on how to do this on here.
You can include a Internet Explorer control in ouyr appliacation. This way you can control which page is displayed and you can call refresh and whatever you want.
Another way is to include a meta refresh tag in the html page so it periodically looks for updates and refreshes ifself.
I see no way to remote control firefox. The only thing i could think of is remember the process id when you start firefox, and the you kill the process and start a new window, but I consider this bad style.

Categories