Beispiel #1
0
        /**
         * 接受服务端消息的线程
         */
        public void ReceiveServerMsg_thread()
        {
            while (true)
            {
                try {
                    MsgData md = cliS.receiveServerMsgData();
                    if (md.cliList.Length > 0)
                    {
                        showOnline(md.cliList);
                    }
                    if (md.msg.Length > 0)
                    {
                        appendToHistory("服务端:\n" + md.msg + "\n");
                    }
                }
                catch (Exception ex) {
                    if (this.IsDisposed)
                    {
                        return;
                    }
                    appendToHistory("服务端 - Receive异常:\n" + ex.Message + "\n");

                    cliS.closeSocket();
                    break;
                }
            }
        }
Beispiel #2
0
        /**
         * 接受服务端信息 -- StreamReader
         */
        public MsgData receiveServerMsgData()
        {
            string  resultStr = streamR.ReadLine();
            MsgData md        = MsgData.DeserializeMsg(resultStr);

            return(md);
        }
Beispiel #3
0
        /**
         * 客户端发送消息
         */
        public void send2Server(MsgData md)
        {
            String mdString = MsgData.SerializeMsg(md);

            streamW.WriteLine(mdString);
            streamW.Flush();
        }
Beispiel #4
0
        /**
         * 序列化为Jison
         */
        public static String SerializeMsg(MsgData md)
        {
            Debug.Write(Json_static.MaxJsonLength);
            string mdString = Json_static.Serialize(md);

            return(mdString);
        }
Beispiel #5
0
        /**
         * 反序列数据
         */
        public static MsgData DeserializeMsg(String mdString)
        {
            MsgData md = Json_static.Deserialize <MsgData>(mdString);

            Debug.WriteLine("");
            return(md);
        }
Beispiel #6
0
        /**
         * 显示信息
         */
        public void showMsg(MsgData md, String remoteip)
        {
            string msg = md.msg;

            Debug.WriteLine("");

            f1.appendToHistory("来自" + remoteip + "\n" + msg + "\n");
        }
Beispiel #7
0
        /**
         * 读取文件内容
         */
        public String readerFile(MsgData md)
        {
            String fileName = md.fileName;
            string str      = "";

            using (StreamReader sr = new StreamReader(fileName)) {
                str = sr.ReadLine();
            }
            return(str);
        }
Beispiel #8
0
        /**
         * 是否接受文件
         */
        public Boolean isFileAccept(MsgData md)
        {
            String msg   = "传来文件" + md.fileName + ", 文件大小" + md.fileSize + "KB\n是否接受?";
            string title = "文件传输";

            if (MessageBox.Show(msg, title, MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                return(true);
            }
            return(false);
        }
Beispiel #9
0
        /**
         * 接受信息
         */
        public void ReceiveMsg(Object obj)
        {
            Socket aSocket = (Socket)obj;

            while (true)
            {
                try {
                    NetworkStream ns        = new NetworkStream(aSocket);
                    StreamReader  sr        = new StreamReader(ns);
                    string        resultStr = sr.ReadLine();
                    MsgData       md        = MsgData.DeserializeMsg(resultStr);
                    string        ipAndPort = aSocket.RemoteEndPoint.ToString();

                    switch (md.type)
                    {
                    case "TEXT":
                        this.showMsg(md, ipAndPort);
                        break;

                    case "FILE":
                        Boolean accecptable = this.isFileAccept(md);
                        if (!accecptable)
                        {
                            continue;
                        }

                        string folderP = FileTranser.getFolderPath();
                        if (folderP.Length == 0)
                        {
                            continue;
                        }

                        this.WriteFile(md, folderP);
                        break;
                    }
                }
                catch (Exception ex) {
                    f1.appendToHistory("receive异常:" + ex.Message + " - ");
                    f1.appendToHistory("断开与" + aSocket.RemoteEndPoint.ToString() + "连接\n");
                    aSocket.Shutdown(SocketShutdown.Both);
                    aSocket.Close();
                    break;
                }
            }
        }
Beispiel #10
0
        /**
         * 写入文件
         */
        public void WriteFile(MsgData md, String localFolder)
        {
            String       fileName = localFolder + @"\" + md.fileName;
            StreamWriter sw       = null;

            try {
                sw = new StreamWriter(fileName);
                sw.WriteLine(md.msg);
            }
            catch (Exception ex) {
                f1.appendToHistory("写文件异常:" + ex.Message);
            }
            finally {
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }
Beispiel #11
0
        /**
         * 发送信息给在线客户端
         * 返回 List<string> failedList
         */
        public List <string> sendMsg2SockeList(String msg, List <String> target)
        {
            List <string> failedList = new List <string>();

            foreach (String ipAndPort in target)
            {
                MsgData md = new MsgData();
                md.msg  = msg;
                md.type = "TEXT";
                string mdString = MsgData.SerializeMsg(md);

                string error = this.sendMsg(mdString, ipAndPort);
                if (error.Length > 0)
                {
                    failedList.Add(error);
                }
            }
            return(failedList);
        }
Beispiel #12
0
        /***************************************************************  发送信息 ***************/
        private void btn_send_Click(object sender, EventArgs e)
        {
            String msg = this.getMsgContent();

            if (msg.Length == 0)
            {
                appendToHistory("请输入内容\n");
                return;
            }

            List <String> target = this.getSendTarget();
            List <string> failed = alive_list.sendMsg2SockeList(msg, target);

            foreach (string error in failed)
            {
                appendToHistory("sendCli异常:" + error);
            }

            //发消息给服务端
            try {
                MsgData md = new MsgData();
                md.msg = msg;
                cliS.send2Server(md);
            }
            catch (Exception ex) {
                appendToHistory("sendServer异常:" + ex.Message);
                if (cliS.so == null)
                {
                    return;
                }
                try {
                    cliS.closeSocket();
                }
                catch (Exception excep) {
                    appendToHistory("close异常" + excep.Message);
                }
            }

            appendToHistory("本机:\n" + msg + "\n");
            tb_msg.Text = "";
        }
Beispiel #13
0
        /**
         * 传文件
         */
        public void translateFile_simple(String ipAndPort, String fileFullPath)
        {
            string filename = System.IO.Path.GetFileName(fileFullPath);

            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileFullPath);
            long fileSize = fileInfo.Length;

            MsgData md = new MsgData();

            md.type     = "FILE";
            md.fileName = filename;
            md.fileSize = fileSize;
            md.msg      = FileTranser.getFileContent(fileFullPath);
            String mdString = MsgData.SerializeMsg(md);

            String error = alive_list.sendMsg(mdString, ipAndPort);

            if (error.Length > 0)
            {
                appendToHistory("文件传输异常:" + error + "\n");
                return;
            }
        }