php与c#的tcp通信问题
好的,我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题在于这一行:
PHP 中的字符串连接运算符是
.
:I think your issue is with this line:
The string concatenation operator in PHP is
.
: