예제 #1
0
        /**
         * 接受服务端信息 -- StreamReader
         */
        public MsgData receiveServerMsgData()
        {
            string  resultStr = streamR.ReadLine();
            MsgData md        = MsgData.DeserializeMsg(resultStr);

            return(md);
        }
예제 #2
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;
                }
            }
        }