Sending and Receiving XML data over TCP

巧了我就是萌 提交于 2019-12-07 16:21:18

问题


I've been trying to figure out how to send and receive XML Data over a TCP Server. I'm coming from a java programming background so i'm a bit out of my depth here. My program works if i'm sending just plain text, however once I try to send the xml data it just hangs. The server never receives the message. I've been searching for code to do this and haven't found any luck, i've seen lots of code samples online that don't work. please if any of you can solve this problem I would be very grateful.

Please I'm looking for code samples here, not explanations on what I should do to fix it. I've only been coding C# for a few days. Here is the sample XML Request.

    <?xml version="1.0" encoding="utf-8"?>
    <ClientRequest>
      <Product>AGENT</Product>
      <Method>GET_SYSTEM_INFO</Method>
      <ClientId>UMOHB</ClientId>
      <Params>
        <Param Value="umohb" Key="username" />
        <Param Value="password" Key="password" />
        <Param Value="localhost" Key="hostname" />
      </Params>
    </ClientRequest>

Here is my TCP Client Code

    public static void sendStringRequest(String hostname, int port, String message)
    {
        String response = String.Empty;
        TcpClient client = getConnection(hostname, port);

        Console.WriteLine(message);

        NetworkStream stream = client.GetStream();
        StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
        writer.AutoFlush = false;
        writer.Write(Encoding.UTF8.GetBytes(message).Length);
        writer.Write(message);
        writer.Flush();

        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        response = reader.ReadLine();

        stream.Close();
    }

回答1:


Don't read until you have flushed the writer.

NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.AutoFlush = false;
writer.Write(Encoding.UTF8.GetBytes(message).Length);
writer.Write(message);
writer.Flush();

StreamReader reader = new StreamReader(stream, Encoding.UTF8);
response = reader.ReadLine();

stream.Close();



回答2:


Try something like this:

public static string sendStringRequest(String hostname, int port, string message) {

 try {
  byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

  TcpClient client = new TcpClient(hostname, port);

  NetworkStream stream = client.GetStream();
  BinaryWriter writer = new BinaryWriter(stream);

  //first 4 bytes - length!
  writer.Write(Convert.ToByte("0"));
  writer.Write(Convert.ToByte("0"));
  writer.Write(Convert.ToByte("0"));
  writer.Write(Convert.ToByte(data.Length));
  writer.Write(data);

  data = new Byte[256];

  // String to store the response ASCII representation.
  String responseData = String.Empty;

  Int32 bytes = stream.Read(data, 0, data.Length);

  responseData = System.Text.Encoding.ASCII.GetString(data, 4, (bytes - 4));

  // Close everything.
  stream.Close();
  client.Close();
  return responseData;
 } catch (ArgumentNullException e) {
  MessageBox.Show("ArgumentNullException: " + e);
  return "null";
 } catch (SocketException e) {
  MessageBox.Show("SocketException: " + e);
  return "null";
 }

}


来源:https://stackoverflow.com/questions/9362959/sending-and-receiving-xml-data-over-tcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!