从套接字读取数据,发送响应并关闭 [英] Read data from socket, send response and close

查看:118
本文介绍了从套接字读取数据,发送响应并关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在哪里PHP脚本打开一个套接字连接到C#程序和C#程序读取数据,然后发送回一个响应一个C#和PHP项目。

I am working on a c# and php project where the PHP script opens a socket to a c# program and the c# program will read the data and then send a response back.

在PHP脚本我有以下几点:

In the PHP script I have the following:

echo "Opening Client";

$fp = fsockopen("127.0.0.1", 12345, $errno, $errstr, 30);

if (!$fp)
{
    echo "Error: $errstr ($errno)<br />";
}
else
{
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: 127.0.0.1\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp))
    {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

在C#项目中,我有以下几点:

In the C# project I have the following:

public void startListen()
        {
            int port = 12345;
            IPAddress serverAddress = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(serverAddress, 12345);
            listener.Start();

            TcpClient client = listener.AcceptTcpClient();

            NetworkStream stream = client.GetStream();
            byte[] data = new byte[client.ReceiveBufferSize];
            int bytesRead = stream.Read(data, 0, Convert.ToInt32(client.ReceiveBufferSize));
            string request = Encoding.ASCII.GetString(data, 0, bytesRead);
            Console.WriteLine(request);

            Console.ReadLine();



PHP脚本似乎停留等待,没有完成,我猜它是因为它在C#应用程序的套接字来发送回一个响应,但我不知道如何做到这一点。另外一个问题,在C#我需要有到Console.ReadLine()否则C#程序将退出,但随后的PHP脚本并完成预期。

The PHP script seems to stay waiting and doesn't finish, I'm guessing its being its because the socket on the c# app to send a response back but I have no idea how to do this. Another problem, in the C# I need to have Console.ReadLine() otherwise the c# program will exit but the PHP Script does then finish as expected.

基本上,我想知道这是读是套接字上发送的数据的最佳方式,是什么让程序运行,所以最好的办法是继续监听套接字上我怎么发回的答复,使PHP脚本可以完成。

Basically, what I want to know is this the best way to read the data that is sent on the socket, what is the best way to keep the program running so it keep on listening on the socket and how I send back a reply so that the php script can finish.

感谢您的帮助,您可以提供。

Thanks for any help you can provide.

推荐答案

我设法弄清楚这一点,处理后的数据,我需要再发送一个stream.write这就是发回的答复。

I managed to figure this out, after processing the data I need to then send a stream.write which is what sends the reply back.

下面是代码

            int port = 12345;
            IPAddress serverAddress = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(serverAddress, port);
            listener.Start();

            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();

                NetworkStream stream = client.GetStream();
                byte[] data = new byte[client.ReceiveBufferSize];
                int bytesRead = stream.Read(data, 0, Convert.ToInt32(client.ReceiveBufferSize));
                string request = Encoding.ASCII.GetString(data, 0, bytesRead);
                Console.WriteLine(request);
                byte[] msg = System.Text.Encoding.ASCII.GetBytes("200 OK");

                // Send back a response.
                stream.Write(msg, 0, msg.Length);
                client.Close();
            }



感谢您的帮助。

Thanks for your help

这篇关于从套接字读取数据,发送响应并关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆