我几乎无法理解C#,我对PHP套接字知之甚少。
我正在翻译下面的代码,将以下c#代码中的文本数据包发送到php。
Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
Client.Connect(new IPEndPoint(IPAddress.Parse(ServerBox.Text), int.Parse(PortBox.Text)));
byte[] message = Encoding.UTF8.GetBytes(MessageAnnouncementBox.Text);
List<byte> pack = new List<byte>();
pack.AddRange(BitConverter.GetBytes((short)(5 + message.Length)));
pack.AddRange(BitConverter.GetBytes((short)11));
pack.AddRange(message);
pack.Add(new byte());
NetworkEngine.Send(Client, pack.ToArray());
NetworkEngine.Send(Client, pack.ToArray());
Client.Disconnect(true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我用PHP套接字中的基本知识进行了测试
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$msg = "5 This is a test announcement";
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 15010);
socket_close($sock);
?>
它让我犯了一个错误。任何建议都会很棒。 TIA