Alternative
01-26-2012, 01:52 AM
I know this forum is for ASP.NET, but I could not find a dedicated C# forum to post this problem in. If I missed the correct forum, please feel free to move it.
I am trying to get C# and PHP to talk with each other via the command prompt.
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace sampServerWatch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool IsValidIP(string addr) {
IPAddress ip;
bool valid = false;
if (string.IsNullOrEmpty(addr))
{
valid = false;
}
else
{
valid = IPAddress.TryParse(addr, out ip);
}
return valid;
}
string[,] ips = {
{"Find server IP and Port...", "", "" },
{"Server1", "46.105.79.69", "7777" },
{"Server2", "8.2.121.99", "7777" },
};
private void Form1_Load(object sender, EventArgs e) {
for (int i = 0; i < ips.Rank + 1; i++) {
cbServers.Items.Add(ips[i, 0]);
}
cbServers.SelectedIndex = 0;
}
private void cbServers_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbServers.SelectedIndex != 0)
{
txtIP.Text = ips[cbServers.SelectedIndex, 1];
txtPort.Text = ips[cbServers.SelectedIndex, 2];
}
else {
txtIP.Text = "";
txtPort.Text = "";
}
}
private void btnWatch_Click(object sender, EventArgs e) {
string ip = txtIP.Text;
string port = txtPort.Text;
if (IsValidIP(ip) && port.Length == 4) {
try
{
IPAddress ipAddr = IPAddress.Parse(ip);
string ipArgument = @"""ip""";
string portArgument = @"""port""";
string call = @"""C:\Program Files/EasyPHP-5.3.8.1/php/php.exe""";
string param2 = "-f";
string param1 = @"""C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php""";
Process theProcess = new Process();
ProcessStartInfo theProcessStartInfo = new ProcessStartInfo(call, "spawn");
theProcessStartInfo.UseShellExecute = false;
theProcessStartInfo.RedirectStandardOutput = true;
string args = string.Format("{0} {1} {2} {3}", call, param1, param2, ip + ":" + port);
MessageBox.Show(args);
theProcessStartInfo.Arguments = args;
theProcess.StartInfo = theProcessStartInfo;
theProcess.Start();
StreamReader theStreamReader = theProcess.StandardOutput;
string theString = "";
while (theStreamReader.ReadLine() != null) {
theString += theStreamReader.ReadLine() + "\n";
}
Console.WriteLine(theString);
rtbDebug.Text = theString;
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex, "Error");
}
}
else {
MessageBox.Show("Error: Invalid IP '" + ip + "' or port '" + port + "'.", "Error");
}
}
}
}
My problem is that it seems theProcessStartInfo.Arguments = args; is not getting all the information that I am specifying in the "string args" declaration. I have tried removing the @ from the param1, param2 and call, passing the variables directly from the Arguments method call, MessageBox.show'ing the values to ensure the arguments are what I expect them to be, and cannot find the reason why this will not work.
Here are the contents of callCMD.php:
<?php
include 'statPageStripped.php';
$inputArg = $argv[1]; #argument 0 is file name.
$output = statPageStripped::writeStats($inputArg);
echo "callCMD: output is " . $output . "\n";
echo $output;
?>
code of statPageStripped.php:
class statPageStripped {
function writeStats($args) {
if (!isset($ip))
$ip = '46.105.79.69';
if (!isset($port))
$port = 7777;
$fp = fsockopen('udp://' . $ip, $port, $errno, $errstr);
if (!$fp)
{
echo "<tr><td bgcolor=\"#2B5486\">Socket Error: $errno - $errstr</td></tr>\n";
}
else
{
$packet = 'SAMP';
$packet .= chr(strtok($ip, '.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr($port & 0xFF);
$packet .= chr($port >> 8 & 0xFF);
fwrite($fp, $packet.'i');
fread($fp, 11);
$is_passworded = ord(fread($fp, 1));
$plr_count = ord(fread($fp, 2));
$max_plrs = ord(fread($fp, 2));
$strlen = ord(fread($fp, 4));
$hostname = fread($fp, $strlen);
$strlen = ord(fread($fp, 4));
$gamemode = fread($fp, $strlen);
$strlen = ord(fread($fp, 4));
$mapname = fread($fp, $strlen);
echo $hostname ."\n";
echo $plr_count . ' / ' . $max_plrs . "\n";
echo $gamemode ."\n";
echo $mapname . "\n";
fwrite($fp, $packet.'c');
fread($fp, 11);
$plr_count = ord(fread($fp, 2));
if ($plr_count > 0)
{
for ($i=0; $i<$plr_count; $i++)
{
$strlen = ord(fread($fp, 1));
$plrname = fread($fp, $strlen);
$score = ord(fread($fp, 4));
echo $plrname . "\n";
}
}
fclose($fp);
}
}
}
Now C# is supposed to send the string "C:\Program Files/EasyPHP-5.3.8.1/php/php.exe" -f "C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php" to the command prompt, and run it. I have used the above command directly in the command prompt and have confirmed it works fine. Which makes me think my C# parameter parsing / passing is the problem.
When I run the code now, it shows a MessageBox.Show window with the correct string that I want the command prompt to have. However after that, it shows the command prompt for a breif second, disappears, and then freezes the application. Any ideas on what the problem may be? If you need more information, let me know.
I am trying to get C# and PHP to talk with each other via the command prompt.
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace sampServerWatch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool IsValidIP(string addr) {
IPAddress ip;
bool valid = false;
if (string.IsNullOrEmpty(addr))
{
valid = false;
}
else
{
valid = IPAddress.TryParse(addr, out ip);
}
return valid;
}
string[,] ips = {
{"Find server IP and Port...", "", "" },
{"Server1", "46.105.79.69", "7777" },
{"Server2", "8.2.121.99", "7777" },
};
private void Form1_Load(object sender, EventArgs e) {
for (int i = 0; i < ips.Rank + 1; i++) {
cbServers.Items.Add(ips[i, 0]);
}
cbServers.SelectedIndex = 0;
}
private void cbServers_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbServers.SelectedIndex != 0)
{
txtIP.Text = ips[cbServers.SelectedIndex, 1];
txtPort.Text = ips[cbServers.SelectedIndex, 2];
}
else {
txtIP.Text = "";
txtPort.Text = "";
}
}
private void btnWatch_Click(object sender, EventArgs e) {
string ip = txtIP.Text;
string port = txtPort.Text;
if (IsValidIP(ip) && port.Length == 4) {
try
{
IPAddress ipAddr = IPAddress.Parse(ip);
string ipArgument = @"""ip""";
string portArgument = @"""port""";
string call = @"""C:\Program Files/EasyPHP-5.3.8.1/php/php.exe""";
string param2 = "-f";
string param1 = @"""C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php""";
Process theProcess = new Process();
ProcessStartInfo theProcessStartInfo = new ProcessStartInfo(call, "spawn");
theProcessStartInfo.UseShellExecute = false;
theProcessStartInfo.RedirectStandardOutput = true;
string args = string.Format("{0} {1} {2} {3}", call, param1, param2, ip + ":" + port);
MessageBox.Show(args);
theProcessStartInfo.Arguments = args;
theProcess.StartInfo = theProcessStartInfo;
theProcess.Start();
StreamReader theStreamReader = theProcess.StandardOutput;
string theString = "";
while (theStreamReader.ReadLine() != null) {
theString += theStreamReader.ReadLine() + "\n";
}
Console.WriteLine(theString);
rtbDebug.Text = theString;
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex, "Error");
}
}
else {
MessageBox.Show("Error: Invalid IP '" + ip + "' or port '" + port + "'.", "Error");
}
}
}
}
My problem is that it seems theProcessStartInfo.Arguments = args; is not getting all the information that I am specifying in the "string args" declaration. I have tried removing the @ from the param1, param2 and call, passing the variables directly from the Arguments method call, MessageBox.show'ing the values to ensure the arguments are what I expect them to be, and cannot find the reason why this will not work.
Here are the contents of callCMD.php:
<?php
include 'statPageStripped.php';
$inputArg = $argv[1]; #argument 0 is file name.
$output = statPageStripped::writeStats($inputArg);
echo "callCMD: output is " . $output . "\n";
echo $output;
?>
code of statPageStripped.php:
class statPageStripped {
function writeStats($args) {
if (!isset($ip))
$ip = '46.105.79.69';
if (!isset($port))
$port = 7777;
$fp = fsockopen('udp://' . $ip, $port, $errno, $errstr);
if (!$fp)
{
echo "<tr><td bgcolor=\"#2B5486\">Socket Error: $errno - $errstr</td></tr>\n";
}
else
{
$packet = 'SAMP';
$packet .= chr(strtok($ip, '.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr(strtok('.'));
$packet .= chr($port & 0xFF);
$packet .= chr($port >> 8 & 0xFF);
fwrite($fp, $packet.'i');
fread($fp, 11);
$is_passworded = ord(fread($fp, 1));
$plr_count = ord(fread($fp, 2));
$max_plrs = ord(fread($fp, 2));
$strlen = ord(fread($fp, 4));
$hostname = fread($fp, $strlen);
$strlen = ord(fread($fp, 4));
$gamemode = fread($fp, $strlen);
$strlen = ord(fread($fp, 4));
$mapname = fread($fp, $strlen);
echo $hostname ."\n";
echo $plr_count . ' / ' . $max_plrs . "\n";
echo $gamemode ."\n";
echo $mapname . "\n";
fwrite($fp, $packet.'c');
fread($fp, 11);
$plr_count = ord(fread($fp, 2));
if ($plr_count > 0)
{
for ($i=0; $i<$plr_count; $i++)
{
$strlen = ord(fread($fp, 1));
$plrname = fread($fp, $strlen);
$score = ord(fread($fp, 4));
echo $plrname . "\n";
}
}
fclose($fp);
}
}
}
Now C# is supposed to send the string "C:\Program Files/EasyPHP-5.3.8.1/php/php.exe" -f "C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php" to the command prompt, and run it. I have used the above command directly in the command prompt and have confirmed it works fine. Which makes me think my C# parameter parsing / passing is the problem.
When I run the code now, it shows a MessageBox.Show window with the correct string that I want the command prompt to have. However after that, it shows the command prompt for a breif second, disappears, and then freezes the application. Any ideas on what the problem may be? If you need more information, let me know.