php与c#的tcp通信问题

发布于 2024-10-12 22:37:22 字数 2310 浏览 2 评论 0原文

好的,我正在开发 ac# 应用程序,该应用程序充当端口 4 上的服务器。我的网站上的 php 脚本充当客户端。它可以连接到服务器,但是,当服务器或客户端尝试发送数据时。结果数据只是随机数和符号。 PHP以下脚本

$msg = $host + " connected;";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $host, 4);
socket_write($socket, $msg, strlen($msg));
socket_close($socket);

C#:

while (heartbeat == false)
                {
                    if (listener.Pending())
                    {
                        heartbeatC = listener.AcceptTcpClient();
                        //heartbeatS = listener.AcceptSocket();
                        NetworkStream heartBeatStream = heartbeatC.GetStream();
                        string heartbeatEP = heartbeatC.Client.RemoteEndPoint.ToString();
                        string heartbeatIP = heartbeatEP.Remove(heartbeatEP.IndexOf(':'), heartbeatEP.Length - heartbeatEP.IndexOf(':'));
                        if (heartbeatIP == Dns.GetHostAddresses("***.********.com")[0].ToString())
                        {
                            dottime.Enabled = false;
                            Console.WriteLine("\nHeartbeat.");
                            bool heartbeatR = false;
                            while (heartbeatR == false)
                            {
                                if (heartBeatStream.DataAvailable)
                                {
                                    //StreamReader sr = new StreamReader(heartBeatStream);
                                    byte[] message = new byte[1024];
                                    int bytesRead = 0;
                                    bytesRead = heartbeatC.Client.Receive(message);
                                    ASCIIEncoding encoder = new ASCIIEncoding();
                                    string msg = encoder.GetString(message);
                                    Console.WriteLine("Server: " + msg);
                                    heartbeatR = true;
                                }
                            }
                            heartbeat = true;
                        }
                        else
                        {
                            heartbeatC.Client.Disconnect(true);
                        }
                    }
                }

Ok, well im working on a c# application that is acting as the server on port 4. And a php script on my website acting as the client. It can connect to the server but, when the server or client attempts to send data. The data turns out to just be random numbers and symbols.
Scripts below

PHP:

$msg = $host + " connected;";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $host, 4);
socket_write($socket, $msg, strlen($msg));
socket_close($socket);

C#:

while (heartbeat == false)
                {
                    if (listener.Pending())
                    {
                        heartbeatC = listener.AcceptTcpClient();
                        //heartbeatS = listener.AcceptSocket();
                        NetworkStream heartBeatStream = heartbeatC.GetStream();
                        string heartbeatEP = heartbeatC.Client.RemoteEndPoint.ToString();
                        string heartbeatIP = heartbeatEP.Remove(heartbeatEP.IndexOf(':'), heartbeatEP.Length - heartbeatEP.IndexOf(':'));
                        if (heartbeatIP == Dns.GetHostAddresses("***.********.com")[0].ToString())
                        {
                            dottime.Enabled = false;
                            Console.WriteLine("\nHeartbeat.");
                            bool heartbeatR = false;
                            while (heartbeatR == false)
                            {
                                if (heartBeatStream.DataAvailable)
                                {
                                    //StreamReader sr = new StreamReader(heartBeatStream);
                                    byte[] message = new byte[1024];
                                    int bytesRead = 0;
                                    bytesRead = heartbeatC.Client.Receive(message);
                                    ASCIIEncoding encoder = new ASCIIEncoding();
                                    string msg = encoder.GetString(message);
                                    Console.WriteLine("Server: " + msg);
                                    heartbeatR = true;
                                }
                            }
                            heartbeat = true;
                        }
                        else
                        {
                            heartbeatC.Client.Disconnect(true);
                        }
                    }
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-10-19 22:37:22

我认为你的问题在于这一行:

$msg = $host + " connected;";

PHP 中的字符串连接运算符是 .

$msg = $host . " connected;";

I think your issue is with this line:

$msg = $host + " connected;";

The string concatenation operator in PHP is .:

$msg = $host . " connected;";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文