【问题标题】:c# array of strings is not getting displayedc# 字符串数组未显示
【发布时间】:2015-09-28 15:03:49
【问题描述】:

以下是用c#编写的一个简单的服务器和客户端代码段。我想从服务器发送一个字符串数组并从客户端接收它并显示在控制台上。但是字符串数组没有显示出来。代码有什么问题吗?

服务器

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace server
{
    class Program
    {
        static void Main(string[] args)
        {  
            TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
            tcpListener.Start();  

            while (true)
            {                     
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                byte[] data = new byte[1024];

                NetworkStream ns = tcpClient.GetStream();
                string[] arr1 = new string[] { "one", "two", "three" };

                var serializer = new XmlSerializer(typeof(string[]));
                serializer.Serialize(tcpClient.GetStream(), arr1);

                int recv = ns.Read(data, 0, data.Length);                 
                string id = Encoding.ASCII.GetString(data, 0, recv);

                Console.WriteLine(id);              
            }               
        }
    }
}

客户

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                byte[] data = new byte[1024];
                string stringData;

                TcpClient tcpClient = new TcpClient("127.0.0.1", 1234);
                NetworkStream ns = tcpClient.GetStream();             

                var serializer = new XmlSerializer(typeof(string[]));
                var stringArr = (string[])serializer.Deserialize(tcpClient.GetStream());

                foreach (string s in stringArr)
                {
                    Console.WriteLine(s);
                }

                string input = Console.ReadLine();
                ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
                ns.Flush();
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }

            Console.Read();
        }
    }
}

【问题讨论】:

  • stringArr 不为空吗?有什么例外吗?
  • 没有异常但字符串数组没有显示在客户端控制台@Kamo
  • clientserver 应用程序运行的触发器是什么?
  • 请更具体@Heinz Siahaan
  • @user5382101 我明白了,所以才问你stringArr反序列化后是否为空。

标签: c# sockets


【解决方案1】:

我运行了你的代码,它挂在客户端的这一行

var stringArr = (string[])serializer.Deserialize(tcpClient.GetStream());

然后我对其进行了修改,因此我首先从 NetworkStream 读取到字节数组,然后使用 MemoryStream 反序列化字节数组。然后它按我的预期工作。

因此,使用带有反序列化的 NetworkStream 可能会出现问题。

XmlSerializer.Deserialize blocks over NetworkStream

查看这个 Stackoverflow 问题,XmlSerializer 似乎会继续尝试从 Stream 读取,直到它到达末尾,这会导致您的问题。

要修复,请按照建议进行操作,并首先将要反序列化的数据读入字节数组。

【讨论】:

  • 你能提供我你修改后的代码吗? @DanL
猜你喜欢
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多