简体   繁体   English

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

[英]Stream for sending byte-arrays and strings (for network)

I'm searching for a Streamclass which contains: - a method for sending/receiving a byte-array - a method for sending/receiving a string 我正在搜索一个包含以下内容的Streamclass:-用于发送/接收字节数组的方法-用于发送/接收字符串的方法

The only Class I've found was NetworkStream. 我发现的唯一类是NetworkStream。 But the disadvantage with the NetworkStream-Class is, that if i want sending a string, i must befor convert this string into a byte-array and send this byte-array, because there is no method for sending strings directly. 但是NetworkStream-Class的缺点是,如果我要发送字符串,我必须将其转换为字节数组并发送此字节数组,因为没有直接发送字符串的方法。 And on the other side classes like Streamwriter have methods for sending/receiving strings, but there have no methods for sending/receiving a byte-array. 另一方面,像Streamwriter这样的类具有发送/接收字符串的方法,但没有发送/接收字节数组的方法。

And if i try to combine these two Streamclasses like this: 如果我尝试将这两个Streamclass结合起来,如下所示:

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

i get a lot of strange errors (like byteArray will not receive on the other side completly), because i'm using here the same one stream in two different ways. 我收到很多奇怪的错误(例如byteArray不会完全在另一端接收),因为我在这里以两种不同的方式使用相同的一个流。

So, must i used NetworkStream-Class for my plan or exists there a better way? 因此,我必须为计划使用NetworkStream-Class还是存在更好的方法?

I had the same problem,and the point is that the other side doesnt know what you are sending byte array or string so what i did is putting a header for each msg specially when dealing with serious server/client application coz you will have multiple data (user info, requesting info,replying info .. etc) 我遇到了同样的问题,关键是另一端不知道您要发送的字节数组或字符串是什么,所以我所做的就是为每个味精添加一个标题,特别是在处理严重的服务器/客户端应用程序时,您将拥有多个数据(用户信息,请求信息,回复信息等)

i am using streamwriter to send and streamreader to receive but i am also using threads the connection remains open as long as the client is connected so i declare them once 我正在使用Streamwriter发送和StreamReader进行接收,但是我也在使用线程,只要客户端已连接,连接就保持打开状态,所以我一次声明它们

here is a full example of my codes 这是我的代码的完整示例

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; }
            }
        }
    }

then at the packet handler i do my handling to check what kind of data the client received 然后在数据包处理程序中,我进行处理以检查客户端收到的数据类型

something like this 像这样的东西

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
                }
        }
    }

i know its kinda messy and not the perfect way to do it , but it works for me and remember that at the other side when sending something you need to add the packet type and subtype , or just any header with any splitter if u doin something simple 我知道它有点杂乱而不是完美的方式,但是它对我有用,并且请记住,在发送内容时,在另一端,您需要添加数据包类型和子类型,或者如果要添加内容,则只需添加带有拆分器的任何标头简单

Finally : i think using Sockets and packets would be much easier if u are sending small packets length 最后:我认为如果您要发送长度很小的数据包,则使用套接字和数据包会容易得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM