用于发送字节数组和字符串的流(用于网络)

本文关键字:用于 网络 字符串 字节 字节数 数组 | 更新日期: 2023-09-27 17:50:17

我正在寻找一个包含:-发送/接收字节数组的方法-一个发送/接收字符串

的方法

我发现的唯一类是NetworkStream。但是NetworkStream-Class的缺点是,如果我想要发送一个字符串,我必须在将这个字符串转换为字节数组并发送这个字节数组之前,因为没有直接发送字符串的方法。另一方面,像Streamwriter这样的类有发送/接收字符串的方法,但没有发送/接收字节数组的方法。

如果我尝试像这样组合这两个streamclass:

TcpClient clientConnection = new TcpClient();
NetworkStream nws = clientConnection.GetStream();
StreamWriter sw = new StreamWriter(nws);
sw.writeLine("ABC");
sw.Flush();
nws.Write(byteArray, 0, lengthToSend);

我得到了很多奇怪的错误(比如byteArray将不会完全接收到另一边),因为我在这里以两种不同的方式使用同一个流。

所以,我必须使用NetworkStream-Class为我的计划或存在一个更好的方法?

用于发送字节数组和字符串的流(用于网络)

我也有同样的问题,关键是对方不知道你发送的是字节数组还是字符串,所以我所做的是为每个消息放一个头,特别是在处理严重的服务器/客户端应用程序时,因为你将有多个数据(用户信息,请求信息,回复信息…等)

我正在使用流写入器发送和流读取器接收,但我也使用线程只要客户端连接,连接就保持打开状态,所以我声明它们一次

这里是我的代码的完整示例

public class Client
{
    private StreamWriter swSender;
    private StreamReader srReceiver;
    private TcpClient tcpServer; 
    private Thread thrMessaging;
    private string UserName = "UK";
    private byte Tries = 0;
    private bool Connected = false;
    public void Connect()
    {
        if (!Connected)
        {
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            string User = localIPs[0].ToString();
            string ServIP = "127.0.0.1";//change this to your server ip
            InitializeConnection(ServIP, User);
        }
        else
        {
            CloseConnection("Disconnected at user's request.");
        }
    }
    private void InitializeConnection(string ServIp, string User)
    {
        IPAddress ipAddr = IPAddress.Parse(ServIp);
        tcpServer = new TcpClient();
        try
        {
            tcpServer.Connect(ipAddr, 1986);//change that 1986 to your server port
        }
        catch 
        { 
        if (Connected) CloseConnection("");
        MessageBox.Show("Connecteing to " + ServIp + "'r'nServer is Down ... Try nomber " + Tries); return; 
        }
        Connected = true;
        UserName = User;
        swSender = new StreamWriter(tcpServer.GetStream());
        swSender.WriteLine(User);
        swSender.Flush();
        thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
        thrMessaging.Start();
    }
    private void ReceiveMessages()
    {
        srReceiver = new StreamReader(tcpServer.GetStream());
        string ConResponse = srReceiver.ReadLine();
        if (ConResponse[0] == '1')
        {

        }
        else
        {
            string Reason = "Not Connected: ";
            Reason += ConResponse.Substring(2, ConResponse.Length - 2);
            return;
        }
        while (Connected)
        {
            try
            {
                string NewMsg = srReceiver.ReadLine();
                if (NewMsg != null && NewMsg != "")
                    PacketHandler.HandlePacket(NewMsg, this);
            }
            catch {  }
        }
    }
    public void CloseConnection(string Reason)
    {
        try
        {
            Connected = false;
            swSender.Close();
            srReceiver.Close();
            tcpServer.Close();
        }
        catch { }
    }
    public void SendMessage(string Msg)
    {
        if (Msg.Length >= 1)
        {
            try
            {
                Tries++;
                swSender.WriteLine(Msg);
                swSender.Flush();
                Tries = 0;
            }
            catch
            {
                if (Tries < 5)
                {
                    try
                    {
                        CloseConnection("No connection made");
                        Connect();
                    }
                    catch { }
                    SendMessage(Msg);
                }
                else { MessageBox.Show("Connecting to server faild for 5 tries"); Tries = 0; }
            }
        }
    }

,然后在包处理程序,我做我的处理,以检查什么样的数据客户端收到

像这样的

public static void HandlePacket(string MsgRec, Client Client)
    {
        string[] Info = MsgRec.Split('|');
        string Type = Info[0];
        if (Type == "")
        {
            return;
        }
        string subtype = Info[1];
        int TLen = Type.Length + subtype.Length + 2;
        string Data = MsgRec.Remove(0, TLen);//this is the main data the server sent
        ushort PacketType = ushort.Parse(Type);
        ushort SubType = ushort.Parse(subtype);
        switch ((Structs.PacketType)PacketType)
        {
            case Structs.PacketType.Login:
                {
                 //do your stuff here
                 break
                }
            case Structs.PacketType.Image:
                {
                 //convert the Data back to byte array then get the image out from it
                 break
                }
            case Structs.PacketType.ByteArray:
                {
                 //convert the Data back to byte array
                 break
                }
        }
    }
我知道这有点乱,不是完美的方法,但它适合我记住,在另一边,当发送一些东西时你需要添加数据包类型和子类型,或者如果你做一些简单的 ,只需添加任何带有拆分器的报头

最后:我认为使用Sockets和数据包会更容易,如果你发送小数据包长度