Server side not reading message correctly

廉价感情. 提交于 2019-12-08 09:39:36

问题


I'm developing a client-server application, where the client is in Android and the server in C#. Before using C# for the server I used autoIT and it worked fine aside from the fact that I had to send messages of 9 characters or else it would get weird symbols.

Now in C# I'm having the same problem. I tried removing all white space but it always leaves something at the start of the message.

Single word messages like:

"SendClose" 

the server reads as

"     SendClose" or " SendClose" if i remove the white spaces

Note the leading white space. It's interesting because when I check the string length it says it is only 11 characters so I don't know what is going on; maybe 2 tabs?

Here's my C# code:

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

public class TcpListenerSample {
  static void Main(string[] args) {
    try {
      // set the TcpListener on port 13000 
      int port = 80;
      IPAddress localAddr = IPAddress.Parse("192.168.1.68");
      TcpListener server = new TcpListener(localAddr, port);

      // Start listening for client requests
      server.Start();


      // Buffer for reading data 
      byte[] bytes = new byte[1024];
      string data;

      //Enter the listening loop 
      while (true) {
        Console.Write("Waiting for a connection... ");

        // Perform a blocking call to accept requests. 
        // You could also user server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("Connected!");

        // Get a stream object for reading and writing
        NetworkStream stream = client.GetStream();

        int i;

        // Loop to receive all the data sent by the client.
        i = stream.Read(bytes, 0, bytes.Length);

        while (i != 0) {
          // Translate data bytes to a ASCII string.
          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          Console.WriteLine(String.Format("Received: {0}", data));

          // Process the data sent by the client.
          data = data.ToUpper();

          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

          // Send back a response.
          stream.Write(msg, 0, msg.Length);
          Console.WriteLine(String.Format("Sent: {0}", data));

          i = stream.Read(bytes, 0, bytes.Length);

        }

        // Shutdown and end connection
        client.Close();
      }
    }
    catch (SocketException e) {
      Console.WriteLine("SocketException: {0}", e);
    }


    Console.WriteLine("Hit enter to continue...");
    Console.Read();
  }

}

回答1:


      data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
      Console.WriteLine(StripExtended(data));        



        static string StripExtended(string arg)
        {
            StringBuilder buffer = new StringBuilder(arg.Length); //Max length
            foreach(char ch in arg)
            {
                UInt16 num = Convert.ToUInt16(ch);//In .NET, chars are UTF-16
                //The basic characters have the same code points as ASCII, and the extended characters are bigger
                if((num >= 32u) && (num <= 126u)) buffer.Append(ch);
            }
            return buffer.ToString();
        }
    }
}


来源:https://stackoverflow.com/questions/30281390/server-side-not-reading-message-correctly

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