【问题标题】:Send XML over TCP connection - only the data is being sent通过 TCP 连接发送 XML - 仅发送数据
【发布时间】:2014-07-01 18:09:14
【问题描述】:

尝试使用 C# 通过 TCP 连接发送数据。

string reply = "<?xml version='1.0' standalone='yes'?>
<root><ele1 type='2'/><author>patrick</author>
<address>spain</address></root>";

这是我认为问题所在:

reply = format.ProcessMessage(message); - this method returns the above string
byte[] breply = System.Text.Encoding.ASCII.GetBytes(reply);
stream.Write(breply, 0, breply.Length);

客户端的响应是:patrickspain 但我希望发送整个 XML 字符串。有人知道如何解决这个问题吗?

以下是我的PHP客户端代码:

<?php

$host    = "127.0.0.1";
$port    = 7890;
$message = "Test Message";
echo "Message To server :".$message;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data\n");
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  : ".$result;
socket_close($socket);

?>

【问题讨论】:

  • 我的猜测是所有数据都在发送,但是您的客户端(或您用来查看响应的工具)正在剥离 Xml 元素。您可以检查客户端收到的 number 个字节吗?
  • 我不知道该怎么做。客户端是一个用 PHP 编码的简单测试应用程序,它只是在浏览器中打印出来自 C# 的响应
  • 我刚刚意识到问题不在于 C#,但我怀疑 PHP 客户端已经剥离了我的 XML

标签: c# php xml tcp


【解决方案1】:

浏览器可以正常接收您的数据。它试图将 XML 显示为 HTML,这意味着您只会看到每个节点的内部文本,在本例中是作者的姓名。

右击页面并点击“查看源代码”查看浏览器收到的内容。您还可以使用 Fiddler 之类的工具来调试 HTTP 流量。

【讨论】:

猜你喜欢
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多