Esempio n. 1
0
        private void ServerRecMsg(object socketClientPara)
        {
            Socket socketServer = socketClientPara as Socket;

            long         fileLength        = 0;
            CmdContainer cmd               = new CmdContainer();
            int          ReceiveBufferSize = 1024 * 1024;

            while (true)
            {
                int    firstReceived = 0;
                byte[] buffer        = new byte[ReceiveBufferSize];


                try
                {
                    if (socketServer != null)
                    {
                        firstReceived = socketServer.Receive(buffer);
                    }

                    if (firstReceived > 0)
                    {
                        if (buffer[0] == '$')
                        {
                            string strRecMsg = Encoding.UTF8.GetString(buffer, 1, firstReceived - 1);


                            if (!cmd.GetCmd(strRecMsg))
                            {
                                continue;
                            }


                            while (cmd.CmdQueue.Count != 0)
                            {
                                string strCmd = cmd.CmdQueue.Dequeue();
                                txtMsg.AppendText("对方发送了:" + strCmd + "\n");
                            }
                        }
                        if (buffer[0] == 2)
                        {
                            string fileNameWithLength = Encoding.UTF8.GetString(buffer, 1, firstReceived - 1);
                            strSRecMsg = fileNameWithLength.Split('-').First();
                            fileLength = Convert.ToInt64(fileNameWithLength.Split('-').Last());
                        }
                        if (buffer[0] == 1)
                        {
                            string         fileNameSuffix = strSRecMsg.Substring(strSRecMsg.LastIndexOf('.'));
                            SaveFileDialog sfDialog       = new SaveFileDialog()
                            {
                                Filter   = "(*" + fileNameSuffix + ")|*" + fileNameSuffix + "",
                                FileName = strSRecMsg
                            };


                            if (sfDialog.ShowDialog(this) == DialogResult.OK)
                            {
                                string savePath = sfDialog.FileName;

                                int  received = 0;
                                long receivedTotalFilelength = 0;
                                bool firstWrite = true;
                                using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                                {
                                    while (receivedTotalFilelength < fileLength)
                                    {
                                        if (firstWrite)
                                        {
                                            fs.Write(buffer, 1, firstReceived - 1);
                                            fs.Flush();

                                            receivedTotalFilelength += firstReceived - 1;

                                            firstWrite = false;
                                            continue;
                                        }
                                        received = socketServer.Receive(buffer);
                                        fs.Write(buffer, 0, received);
                                        fs.Flush();

                                        receivedTotalFilelength += received;
                                    }
                                    fs.Close();
                                }

                                string fName = savePath.Substring(savePath.LastIndexOf("\\") + 1);
                                string fPath = savePath.Substring(0, savePath.LastIndexOf("\\"));
                                txtMsg.AppendText("对方:" + GetCurrentTime() + "\r\n您成功接收了文件" + fName + "\r\n保存路径为:" + fPath + "\r\n");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    txtMsg.AppendText("系统异常消息:" + ex.Message);
                    break;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 接收客户端发来的信息
        /// </summary>
        private void ServerRecMsg(object socketClientPara)
        {
            Socket socketServer = socketClientPara as Socket;

            long         fileLength        = 0;
            CmdContainer cmd               = new CmdContainer();
            int          ReceiveBufferSize = 1024 * 1024;

            while (true)
            {
                int    firstReceived = 0;
                byte[] buffer        = new byte[ReceiveBufferSize];


                try
                {
                    //获取接收的数据,并存入内存缓冲区  返回一个字节数组的长度
                    if (socketServer != null)
                    {
                        firstReceived = socketServer.Receive(buffer);
                    }

                    if (firstReceived > 0)    //接受到的长度大于0 说明有信息或文件传来
                    {
                        if (buffer[0] == '$') //0为文字信息
                        {
                            string strRecMsg = Encoding.UTF8.GetString(buffer, 1, firstReceived - 1);


                            if (!cmd.GetCmd(strRecMsg))
                            {
                                continue;
                            }


                            while (cmd.CmdQueue.Count != 0)
                            {
                                string strCmd = cmd.CmdQueue.Dequeue();
                                txtMsg.AppendText("对方发送了:" + strCmd + "\n");
                            }
                        }
                        if (buffer[0] == 2)//2为文件名字和长度
                        {
                            string fileNameWithLength = Encoding.UTF8.GetString(buffer, 1, firstReceived - 1);
                            strSRecMsg = fileNameWithLength.Split('-').First();                                //文件名
                            fileLength = Convert.ToInt64(fileNameWithLength.Split('-').Last());                //文件长度
                        }
                        if (buffer[0] == 1)                                                                    //1为文件
                        {
                            string         fileNameSuffix = strSRecMsg.Substring(strSRecMsg.LastIndexOf('.')); //文件后缀
                            SaveFileDialog sfDialog       = new SaveFileDialog()
                            {
                                Filter   = "(*" + fileNameSuffix + ")|*" + fileNameSuffix + "", //文件类型
                                FileName = strSRecMsg
                            };

                            //如果点击了对话框中的保存文件按钮
                            if (sfDialog.ShowDialog(this) == DialogResult.OK)
                            {
                                string savePath = sfDialog.FileName; //获取文件的全路径
                                //保存文件
                                int  received = 0;
                                long receivedTotalFilelength = 0;
                                bool firstWrite = true;
                                using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                                {
                                    while (receivedTotalFilelength < fileLength) //之后收到的文件字节数组
                                    {
                                        if (firstWrite)
                                        {
                                            fs.Write(buffer, 1, firstReceived - 1); //第一次收到的文件字节数组 需要移除标识符1 后写入文件
                                            fs.Flush();

                                            receivedTotalFilelength += firstReceived - 1;

                                            firstWrite = false;
                                            continue;
                                        }
                                        received = socketServer.Receive(buffer); //之后每次收到的文件字节数组 可以直接写入文件
                                        fs.Write(buffer, 0, received);
                                        fs.Flush();

                                        receivedTotalFilelength += received;
                                    }
                                    fs.Close();
                                }

                                string fName = savePath.Substring(savePath.LastIndexOf("\\") + 1); //文件名 不带路径
                                string fPath = savePath.Substring(0, savePath.LastIndexOf("\\"));  //文件路径 不带文件名
                                txtMsg.AppendText("对方:" + GetCurrentTime() + "\r\n您成功接收了文件" + fName + "\r\n保存路径为:" + fPath + "\r\n");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    txtMsg.AppendText("系统异常消息:" + ex.Message);
                    break;
                }
            }
        }