gpt4 book ai didi

c# - php和c#的tcp通讯问题

转载 作者:可可西里 更新时间:2023-11-01 02:40:21 25 4
gpt4 key购买 nike

好吧,我正在开发一个 c# 应用程序,该应用程序充当端口 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);
}
}
}

最佳答案

我认为您的问题出在这一行:

$msg = $host + " connected;";

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

$msg = $host . " connected;";

关于c# - php和c#的tcp通讯问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4709199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com