【问题标题】:C#-PHP socket connectionC#-PHP 套接字连接
【发布时间】:2012-04-05 01:50:13
【问题描述】:

我正在通过服务器上的 php 脚本控制的 pc 上编写程序。我目前正在使用 php 来回 ftp 文件,并使用 c# 读取文件并根据文件中的数据执行命令。然而,这不是一个理想的解决方案。

我想看一个关于如何使用 php 将数据发送到 c# 程序 pver 套接字的教程或示例。

我要发送的数据示例

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

谁能指出我正确的方向?

【问题讨论】:

    标签: c# php sockets


    【解决方案1】:

    与其尝试让您的服务器端 PHP 脚本向 C# 程序发送数据,这会让您头疼不已,不如在 PHP 脚本上写一些东西,给定页面的特定请求,输出当前排队的指令?然后,C# 程序可以向页面发出 WebRequest 并接收其指令。

    例如:

    == PHP 脚本 ==

    <?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";
        }
    ?>
    

    现在从请求中实际检索数据:

    == C# 客户端代码 ==

    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;
    }
    

    这是一个错误处理最少的粗略模板,希望它足以让您入门。

    编辑:糟糕,忘记包含示例用法:

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

    【讨论】:

      【解决方案2】:

      您可以使用 PHP 的 soap extensions 创建一个 SOAP WebService,您可以从 C# 轻松调用它。就像您输入了访问权限一样,您不必创建自己的协议。

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 2015-04-06
        • 2011-05-31
        • 1970-01-01
        • 2011-02-05
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多