Socket ClientSocket;  // 套接字声明
        // 接受并创建文件
        public void Create()
        {
            Socket client = ClientSocket;  // 获得客户端套接字
            //获得客户端节点对象
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

            //获得[文件名]
            string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

            // 根据时间生成文件名
            SendFileName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + SendFileName.Substring(SendFileName.IndexOf('.'));

            //获得[包的大小]
            string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

            //获得[包的总数量]
            int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));

            //获得[最后一个包的大小]
            string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));


            //创建一个新文件
            FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write);

            //已发送包的个数
            int SendedCount = 0;

            while (true)
            {
                byte[] data = TransferFiles.ReceiveVarData(client);
                if (data.Length == 0)
                {
                    break;
                }
                else
                {
                    SendedCount++;
                    //将接收到的数据包写入到文件流对象
                    MyFileStream.Write(data, 0, data.Length);
                    //显示已发送包的个数
                }
            }
            //关闭文件流
            MyFileStream.Close();
            string ip = IPAddress.Parse(((IPEndPoint)client.RemoteEndPoint).Address.ToString()).ToString();

            //关闭套接字
            client.Close();
            //文件路径
            string filepath = Application.StartupPath.ToString() + "\\" + SendFileName;

            this.Invoke((EventHandler)(delegate
            {
                if (MessageBox.Show("收到来自" + ip + "的图片,保存路径为:" + filepath + "\r\n是否打开?", "提醒", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    FormReceive re = new FormReceive();
                    re.path = filepath;
                    re.Show();
                }
            }));
        }
        int PacketSize = 5000;  // 默认包大小是5000
        //发送文件的线程
        private void StartSend()
        {
            //创建一个文件对象
            FileInfo EzoneFile = new FileInfo("temp.bmp");  // 发送缓存文件 已经是嘉隐藏了信息了的
            //打开文件流
            FileStream EzoneStream = EzoneFile.OpenRead();

            //包的数量
            int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));

            //最后一个包的大小
            int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));

            //指向远程服务端节点
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(textBoxIP.Text.Trim()), port);

            try
            {
                //创建套接字
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //连接到服务器端
                client.Connect(ipep);

                //发送[文件名]到客户端
                TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
                //发送[包的大小]到客户端
                TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
                //发送[包的总数量]到客户端
                TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
                //发送[最后一个包的大小]到客户端
                TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));

                //数据包
                byte[] data = new byte[PacketSize];
                //开始循环发送数据包
                for (int i = 0; i < PacketCount; i++)
                {
                    //从文件流读取数据并填充数据包
                    EzoneStream.Read(data, 0, data.Length);
                    //发送数据包
                    TransferFiles.SendVarData(client, data);
                }

                //如果还有多余的数据包,则应该发送完毕!
                if (LastDataPacket != 0)
                {
                    data = new byte[LastDataPacket];
                    EzoneStream.Read(data, 0, data.Length);
                    TransferFiles.SendVarData(client, data);
                }

                //关闭套接字
                client.Close();

                //关闭文件流
                EzoneStream.Close();
                this.Invoke((EventHandler)(delegate
                {
                    MessageBox.Show("发送完毕!");
                    textBoxInfo.Text = "";
                }));
            }
            catch (Exception ex)
            {
                this.Invoke((EventHandler)(delegate
                {
                    MessageBox.Show("连接接收方失败,接收方是否运行?\r\n" + ex.Message);
                }));
            }
        }