rb622
rb622
2017-09-01 08:26

c# Socket,tcp接收时会粘包?

  • tcp
  • c#
  • 线程
  • socket

private void RecMsg(object sokConnectionparn)
{
Socket sokClient = sokConnectionparn as Socket;
while (true)
{

            byte[] arrMsgRec = new byte[1024 * 1024 * 3+3];
            // 将接受到的数据存入到输入  arrMsgRec中;
            int length = -1;
            try
            {
                length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;

            }
            catch (SocketException se)
            {
                ShowMsg("异常:" + se.Message);

                dictSocket.Remove(sokClient.RemoteEndPoint.ToString());

                dictThread.Remove(sokClient.RemoteEndPoint.ToString());

                DeleteLib(sokClient.RemoteEndPoint.ToString());

                break;
            }
            catch (Exception e)
            {
                ShowMsg("异常:" + e.Message);
                dictSocket.Remove(sokClient.RemoteEndPoint.ToString());
                dictThread.Remove(sokClient.RemoteEndPoint.ToString());
                DeleteLib(sokClient.RemoteEndPoint.ToString());
                break;
            }
            try
            {
                if (arrMsgRec[0] == 0)  // 表示接收到的是数据;
                {
                    string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);// 将接受到的字节数据转化成字符串;
                    ShowMsg(strMsg);
                }
                if (arrMsgRec[0] == 1) // 表示接收到的是文件;
                {
                    filesource = sokClient.RemoteEndPoint.ToString();

                    //var i = BitConverter.ToInt32(arrMsgRec, arrMsgRec.Length-3);
                    if (System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1).Contains("FileName"))
                    {
                        string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);
                        bpp.FileName = strMsg.Split(':')[1];
                        bpp.Index = 0;
                        savepath = "";
                        savepath = Path.Combine(savepath, bpp.FileName);
                    }
                    else if (System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1).Contains("PackageCount"))
                    {
                        string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);
                        bpp.PackageCount = int.Parse(strMsg.Split(':')[1]);
                    }

                    else if (arrMsgRec[1] == 22 && arrMsgRec[2] == 33)
                    {
                        //int i = BitConverter.ToInt32(arrMsgRec, arrMsgRec.Length - 3);
                        byte[] buffer = new byte[length - 3];
                        Buffer.BlockCopy(arrMsgRec, 3, buffer, 0, buffer.Length);
                        if (!string.IsNullOrEmpty(bpp.FileName))
                        {

                            Thread fw = new Thread(() => FileWrite(savepath, bpp.Index, 1024 * 1024 * 3, buffer.Length, buffer));
                            fw.IsBackground = true;
                            fw.Start();
                        }
                    }
                    else
                    {
                        stringtobyte("Err:" + bpp.Index.ToString(), 1);
                        dictSocket[filesource].Send(stringtobyte("Err:" + bpp.Index.ToString(), 1));
                    }

                }
            }
            catch(Exception ex)
            {
                //MessageBox.Show(ex.Message);

            }
        }
    }
            用这个作为后台线程接收,然后发文件(客户端线程分包自动发的)的同时传输信息的话会粘包吧,好像是这个说法,就是那一包文件数据会收不到,然后聊天信息会出现乱码,求教怎么解决,新手实在搞不定- -谢谢.
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答