C# 为什么我的服务器接收的是字节数组而不是字符串?传输控制协议

C# 为什么我的服务器接收的是字节数组而不是字符串?传输控制协议,c#,.net,sockets,tcp,buffer,C#,.net,Sockets,Tcp,Buffer,所以我有一个客户端,我无法访问它的源代码,但是客户端是超基本的,它连接到我的服务器,向我的客户端发送一个类似于A8745783-K8757853的字符串 然后我的代码接收到它,如下所示: 如何将其存储为类似于此A8745783-K8757853的字符串我希望收到的保留值为A8745783-K8757853,而不是a/06/05/02/06 我不知道为什么我的服务器会这样存储它,这就是我想知道的,我如何正确地接收作为字符串的缓冲区 public class Connection {

所以我有一个客户端,我无法访问它的源代码,但是客户端是超基本的,它连接到我的服务器,向我的客户端发送一个类似于A8745783-K8757853的字符串

然后我的代码接收到它,如下所示:

如何将其存储为类似于此A8745783-K8757853的字符串我希望收到的保留值为A8745783-K8757853,而不是a/06/05/02/06

我不知道为什么我的服务器会这样存储它,这就是我想知道的,我如何正确地接收作为字符串的缓冲区

public class Connection
{
        private const int Port = 12345;

        public static List<string[]> CardList = new List<string[]>();
        public static List<string[]> UserList = new List<string[]>();
        public static List<string[]> ReceivedList = new List<string[]>();

        //Make them private because we don't want to mess with these, it's "almost" like having them in a const form.. Keyword "Almost".
        private static readonly IPAddress Ip = IPAddress.Parse("127.0.0.1");
        private static TcpListener _listener;
        private static TcpClient _client;
        private static NetworkStream _nwStream;

        public static int BytesAmount;
        public static byte[] Buffer;

        public Connection()
        {
            _listener = new TcpListener(Ip, Port);
        }

        public static string User { get; set; }
        public static string Card { get; set; }

        public static string UserData { get; set; }
        public static string Received { get; set; }

        public async Task StartListenAsync()
        {
            _listener.Start();

            // This will sit in idle, waiting for a connection..
            Console.WriteLine("Listening..");

            // Assign the first connection to the client obj, we're doing this async.
            _client = await _listener.AcceptTcpClientAsync();
            _nwStream = _client.GetStream();

            if (_client.Connected)
                Console.WriteLine("Client has connected!");

            // Keep listening for incoming data and split it.
            while (true)
                if (_nwStream.DataAvailable)
                {
                    //long i = _nwStream.Length;
                    //Console.WriteLine(i);

                    Buffer = new byte[_client.ReceiveBufferSize];
                    BytesAmount = _nwStream.Read(Buffer, 0, _client.ReceiveBufferSize);
                    Received = Encoding.UTF8.GetString(Buffer, 0, BytesAmount);

                    string something = Received;

                    // Our IDE (Visual Studio (VS) knows to make this into a string array because we're calling split on a string
                    // So to not make it redundant we'll be fine by doing new[] which in this case represents a new string array)
                    var splitArray = Received.Split(new[] { "-" }, StringSplitOptions.None);

                    User = splitArray[0].Replace(" ", "");
                    Card = splitArray[1].Replace(" ", "");

                    Console.WriteLine("Testing 1: " + User);
                    Console.WriteLine("Testing 2: " + Card);

                    Console.WriteLine("Received: " + Received);
                    Console.WriteLine("Sending back : " + Received);
                    _nwStream.Write(Buffer, 0, BytesAmount);
                }
        }
    }

所以问题在于编码,我想用

Unicode
Recieved = Encoding.Unicode.GetString(Buffer, 0, BytesAmount);

您在哪里看到未知程序向您发送字符串A8745783-K8757853,而不是UTF8?您可以使用Wireshark确认TCP流中实际发送的内容吗?也许客户端会对接收到的字节序列进行进一步的转换,或者用不同的编码对其进行解释?您对UTF8中的字符串有多确定?可能是UTF 16,所以有额外的前导/尾随零?或者它是上个世纪的代码页?如果您不确定编码是什么,则无法解释字符串。或者甚至找出它的结尾:发送字符串的客户端是我的老师给我的,我一直在和我的老师核实这一点,他认为这可能是我接收字节的方式,可能是问题PO警报:接收/接收的拼写正确,而不是接收/接收。所以你说虽然@Christopher是正确的,但我宁愿回答自己而不是尊敬他