简体   繁体   中英

C#-PHP socket connection

I am writing a program on a pc that is controlled via a php script on a server. I am currently using php to ftp a file back and forth and using the c# to read the file and execute commands based on the data in the file. However this is not an ideal solution.

I would like to see a tutorial or example on how to use php to send data to ac# program pver sockets.

Example of data I would like to send

1:control1
1:control2
1:control3
0:control4
0:control5

Can anyone point me in the right direction?

Rather than try and get your server-sided PHP script to send data to the C# program, which will give you a heap of headaches, why not write something on the PHP script that, given a specific request to the page, outputs the currently queued instructions? The C# program can then just make a WebRequest to the page and receive its instructions.

For example:

== PHP SCRIPT ==

<?php
    //main execution.
    process_request();

    function process_request()
    {
        $header = "200 OK";
        if (!empty($_GET['q']) && validate_request())
        {
            switch ($_GET['q'])
            {
                case "get_instructions":
                    echo get_instructions();
                    break;
                case "something_else":
                    //do something else depending on what data the C# program requested.
                    break;
                default:
                    $header = "403 Forbidden"; //not a valid query.
                    break;
            }
        }
        else { $header = "403 Forbidden"; } //invalid request.
        header("HTTP/1.1 $header");
    }

    function validate_request()
    {
        //this is just a basic validation, open to you for how you want to validate the request, if at all.
        return $_SERVER["HTTP_USER_AGENT"] == "MyAppName/1.1 (Instruction Request)";
    }

    function get_instructions()
    {
                    //pseudo function, for example purposes only.
        return "1:control1\n1:control2\n1:control3\n0:control4\n0:control5";
    }
?>

Now to actually retrieve data from the request:

== C# Client Code ==

private string QueryServer(string command, Uri serverpage)
{
    string qString = string.Empty;

    HttpWebRequest qRequest = (HttpWebRequest)HttpWebRequest.Create(serverpage.AbsoluteUri + "?q=" + command);
    qRequest.Method = "GET";
    qRequest.UserAgent = "MyAppName/1.1 (Instruction Request)";

    using (HttpWebResponse qResponse = (HttpWebResponse)qRequest.GetResponse())
        if (qResponse.StatusCode == HttpStatusCode.OK)
            using (System.IO.StreamReader qReader = new System.IO.StreamReader(qResponse.GetResponseStream()))
                qString = qReader.ReadToEnd().Trim(); ;

    return qString;
}

This is a rough template with minimal error handling, hopefully it's enough to get you started.

EDIT: Woops, forgot to include an example usage:

MessageBox.Show(QueryServer("get_instructions", new Uri("http://localhost/interop.php")));

you could use the soap extensions for PHP to create a SOAP WebService which you can call easily from you C#. Like that you have typed access and you dont have to create your own protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM