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

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

Related

Socket.IO & Express alternative for C#?

I'm looking to see if it's possible to create an application using C# that creates a local web server and allows me to pass information from the server, to the client website.
I've been using Node.js with Express to create a local web server and then using Socket.io to pass information through to the client, to display in realtime with Javascript. Only issue is I'm more comfortable with C# and I'd like to distribute this application, with Node Modules and Electron the app is clocking in at around 150MB, it's also many files and folders as opposed to just a .exe
Details of Application:
Reads data from log files
Decodes Json inside files
Sends specific data to website
Client receives data and displays
I've managed to get halfway there by using HttpListener, but from what I understand I cant send data to it? So I figured I could edit the html before I sent it and have yet to setup the FindDivByID method
TLDR; Is there a way to create a Local Web Server (Application) that is able to send data to the Client Website.
EDIT: Thanks for the suggestion, though I'm hoping to keep it all down to one distributable application, that reads the data from the local PC, creates the web server and sends to the clients
Well, if you wanna go full C#, I'd recommend SignalR, very solid
https://www.asp.net/signalr
Alternatively, you could keep your Socket.IO server in Node.JS and use this Socket.IO C# client library to interface with it (although I never really did tried)
https://github.com/Quobject/SocketIoClientDotNet

Upload contents of csv file from sftp folder to cloud SQL Server database - best practice

I am trying to find the best way to take a csv file (20-30MB) from an sftp folder, and upload the data to a cloud based SQL Server. I'm just looking for the best approach, not looking for someone to write the code for me.
As of now I have this working (which fails 50% of the time) the following way:
On local PC, setup HTTPWEBREQUEST, convert file to byte array, send byte array via Stream.
On web server, receive stream, convert and save to temp folder on webserver.
Then have local PC send another message to web server that starts the following -- grab saved file on server, convert file to datatable... then execute a SqlBulkCopy, finally delete file on server.
This process fails half the time. I have created a new WebApi service using authentication and I would like to find the best way to do this using the new service I am creating. In my research sometimes I find information on POST a single item to the database, not sure if I should be doing something like that to 20,000 rows of data.
Can someone please point me in the right direction. Obviously what I am doing is not the best way if it keeps failing.

Passing data from a javascript function in my remote website to a c# application on my desktop application

I have a dynamic program. These are the steps that the program follows.
i) I have a windows form application and I have divided the window into two parts. One part contains a ChromiumWebBrowser where it loads a remote website (like www.abc.com). The other part does normal operations of fetching data from a wampserver MYSQL DATABASE installed on the local computer and gives output on the same window division.
ii) Now I use the Chromium web browser to import data from the remote website and send it to a local php file in the local wampserver and then inside the php file I send the data to appropriate local database tables.
iii) When the remote website loads, there is a button "Import data" and then this importation is processed via an AJAX call and now it is in the ajax success: function(){ } that I send the data to my local php file. (I have control over both remote and local wampservers)
iv) NOW MY QUESTION IS how do I pass the data from the external javascript ajax to my c# application so that I don't need to have the local php file i.e. The received data will be sent to database directly from c#
v) And are there any security threats in the process?
I hope I'm was clear, Any suggestions are welcome
Easiest option is to implement a custom scheme. So for example you would make your requests to custom://ajax/uploadToDatabase. You can then parse the request, update the database and respond accordingly.
http://rawgit.com/cefsharp/CefSharp/master/CefSharp.Example/Resources/Home.html#features-custom-schemes
https://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp.Example/CefSharpSchemeHandler.cs#L51
The CefSharp.WinForms.Example and CefSharp.Wpf.Example projects both provide a working implementation. They're available on GitHub
https://github.com/cefsharp/CefSharp

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.

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

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.

Categories