用PHP读取TCP流(转换C#代码)
我在获取/读取 TCP 服务器的任何响应时遇到问题。 连接建立良好,我正在向服务器写入数据,但该服务器所需的特定 EOT 字符 (0x04) 可能存在问题。
另外,我给出的示例代码(见下文 - 在 C# 中)将字符串转换为二进制/字节,从我读到的内容来看,PHP 在使用 fsockopen & 时不需要这样做。 fputs。
我的代码:
$host = "xxx.xxx.xxx.xxx";
$port = 23432;
$msg = "sample message". chr(4);
$fp = fsockopen ($host, $port, $errno, $errstr);
if (!$fp) {
$lines = "Error: could not open socket connection";
} else {
// write the user string to the socket
fputs ($fp, $msg);
// get the result
$lines .= fgets($fp, 1024);
// close the connection
fclose($fp);
} ?>
Server said: <b><?php echo $lines; ?>
我得到的示例 C# 代码:
TcpClient tcp = new TcpClient();
tcp.Connect("xxx.xxx.xxx.xxx", 23432);
Console.WriteLine();
Console.WriteLine("Connected to tcp server.");
// Sent request to tcp server
NetworkStream stream = tcp.GetStream();
// Ask for travel time data
byte[] tx = GetBytes("sample message");
stream.Write(tx, 0, tx.Length);
// Receive data from tcp server
byte[] buffer = new byte[0xffff];
int bytesRead = stream.Read(buffer, 0, 0xffff);
Console.WriteLine("Received " + bytesRead + " bytes from tcp server");
string s = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Below is the data received.");
Console.WriteLine(s);
// Keep the connection open, the data is coming every 20 secs
tcp.Close();
// This function will add the binary byte (0x04) to the end of the text
static private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length + 1];
Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, 0);
bytes[str.Length] = 0x04;
return bytes;
}
有什么想法吗?目前,PHP 代码在发送消息后停留了大约 40 秒,没有返回任何内容。
I'm having problems getting/reading any response from a TCP server.
The connection is establishing fine and I'm writing to the server OK but there could be an issue with the particular EOT character required by this server (0x04).
Also the example code I've been given (see below - in C#) converts the string to binary/bytes, which from what I've read PHP doesn't require when using fsockopen & fputs.
My code:
$host = "xxx.xxx.xxx.xxx";
$port = 23432;
$msg = "sample message". chr(4);
$fp = fsockopen ($host, $port, $errno, $errstr);
if (!$fp) {
$lines = "Error: could not open socket connection";
} else {
// write the user string to the socket
fputs ($fp, $msg);
// get the result
$lines .= fgets($fp, 1024);
// close the connection
fclose($fp);
} ?>
Server said: <b><?php echo $lines; ?>
The example C# code I was given:
TcpClient tcp = new TcpClient();
tcp.Connect("xxx.xxx.xxx.xxx", 23432);
Console.WriteLine();
Console.WriteLine("Connected to tcp server.");
// Sent request to tcp server
NetworkStream stream = tcp.GetStream();
// Ask for travel time data
byte[] tx = GetBytes("sample message");
stream.Write(tx, 0, tx.Length);
// Receive data from tcp server
byte[] buffer = new byte[0xffff];
int bytesRead = stream.Read(buffer, 0, 0xffff);
Console.WriteLine("Received " + bytesRead + " bytes from tcp server");
string s = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Below is the data received.");
Console.WriteLine(s);
// Keep the connection open, the data is coming every 20 secs
tcp.Close();
// This function will add the binary byte (0x04) to the end of the text
static private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length + 1];
Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, 0);
bytes[str.Length] = 0x04;
return bytes;
}
Any ideas? At the moment the PHP code sits there for about 40 seconds after sending the message and returns nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论