好的,我正在开发一个作为端口4服务器的c#应用程序。我网站上的一个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);
}
}
}
答案 0 :(得分:3)
我认为你的问题在于这一行:
$msg = $host + " connected;";
PHP中的字符串连接运算符是.
:
$msg = $host . " connected;";