Ejemplo n.º 1
0
        /// <summary>
        /// 接收文件
        /// </summary>
        /// <param name="sokConnectionparn"></param>
        public void RecFile(Socket sokClient)
        {
            byte[] buffer = new byte[1024];
            sokClient.Receive(buffer);
            long   receive = 0L, length = BitConverter.ToInt64(buffer, 0);
            string fileName = Encoding.Default.GetString(buffer, 0, sokClient.Receive(buffer));

            // Console.WriteLine("Receiveing file:" + fileName + ".Plz wait...");
            using (FileStream writer = new FileStream(Path.Combine(@"D:\recv", fileName), FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int received;
                //断点接受 在这里判断设置writer.Position即可
                while (receive < length)
                {
                    received = sokClient.Receive(buffer);
                    writer.Write(buffer, 0, received);
                    writer.Flush();
                    receive += (long)received;
                    //Console.WriteLine("Received " + receive + "/" + length + ".");//进度
                }
            }
            ShwMsgForView.ShwMsgforView(lstbxMsgView, "文件保存成功:" + fileName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="sokConnectionparn"></param>
        void RecMsg(object sokConnectionparn)
        {
            Socket sokClient = sokConnectionparn as Socket;

            while (true)
            {
                // 定义一个2M的缓存区;
                byte[] arrMsgRec = new byte[1024 * 1024 * 4];
                // 将接受到的数据存入到输入  arrMsgRec中;
                int length = -1;
                try
                {
                    length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
                }
                catch (SocketException se)
                {
                    ShowMsg("异常:" + se.Message);
                    // 从 通信套接字 集合中删除被中断连接的通信套接字;
                    dict.Remove(sokClient.RemoteEndPoint.ToString());
                    // 从通信线程集合中删除被中断连接的通信线程对象;
                    dictThread.Remove(sokClient.RemoteEndPoint.ToString());
                    // 从列表中移除被中断的连接IP
                    lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString());
                    break;
                }
                catch (Exception e)
                {
                    ShowMsg("异常:" + e.Message);
                    // 从 通信套接字 集合中删除被中断连接的通信套接字;
                    dict.Remove(sokClient.RemoteEndPoint.ToString());
                    // 从通信线程集合中删除被中断连接的通信线程对象;
                    dictThread.Remove(sokClient.RemoteEndPoint.ToString());
                    // 从列表中移除被中断的连接IP
                    lbOnline.Items.Remove(sokClient.RemoteEndPoint.ToString());
                    break;
                }
                if (arrMsgRec[0] == 0)                                                             // 表示接收到的是数据;
                {
                    string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1); // 将接受到的字节数据转化成字符串;
                    FileName = strMsg;
                    ShowMsg(strMsg);
                }
                if (arrMsgRec[0] == 1) // 表示接收到的是文件;
                {
                    //   SaveFileDialog sfd = new SaveFileDialog();

                    //      if (sfd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    //        {// 在上边的 sfd.ShowDialog() 的括号里边一定要加上 this 否则就不会弹出 另存为 的对话框,而弹出的是本类的其他窗口,,这个一定要注意!!!【解释:加了this的sfd.ShowDialog(this),“另存为”窗口的指针才能被SaveFileDialog的对象调用,若不加thisSaveFileDialog 的对象调用的是本类的其他窗口了,当然不弹出“另存为”窗口。】

                    /*      string fileSavePath = "D:\\recv\\"+FileName;// 获得文件保存的路径;
                     *    // 创建文件流,然后根据路径创建文件;
                     *    using (FileStream fs = new FileStream(fileSavePath, FileMode.Create))
                     *    {
                     *        fs.Write(arrMsgRec, 1, length - 1);
                     *        ShowMsg("文件保存成功:" + fileSavePath);
                     *    }*/
                    byte[] buffer = new byte[1024];
                    sokClient.Receive(buffer);
                    long   receive = 0L, length1 = BitConverter.ToInt64(buffer, 0);
                    string fileName = Encoding.Default.GetString(buffer, 0, sokClient.Receive(buffer));
                    // Console.WriteLine("Receiveing file:" + fileName + ".Plz wait...");
                    using (FileStream writer = new FileStream(Path.Combine(@"D:\recv", fileName), FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        int received;
                        //断点接受 在这里判断设置writer.Position即可
                        while (receive < length1)
                        {
                            received = sokClient.Receive(buffer);
                            writer.Write(buffer, 0, received);
                            writer.Flush();
                            receive += (long)received;
                            //Console.WriteLine("Received " + receive + "/" + length + ".");//进度
                        }
                    }
                    ShwMsgForView.ShwMsgforView(lstbxMsgView, "文件保存成功:" + fileName);
                }


                //  }
            }


            //RecFile(sokConnectionparn);
        }