• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 124,  comments - 29,  trackbacks - 0

            調(diào)用過程:
            //初始化  始終開著服務(wù)器端進(jìn)程監(jiān)聽有沒有發(fā)消息過來的客戶      
            private void MutualWnd_Load(object sender, EventArgs e)
                    {
                        #region 開啟服務(wù)端
                        CheckForIllegalCrossThreadCalls = false;
                        server = new ChatServer(this);
                        server.Start();
                        #endregion
                    }

            //發(fā)送
                    private void btnSend_Click(object sender, EventArgs e)
                    {
                        if (curClient == null)
                        {
                            ChatClient client = new ChatClient(this, curUserIP, curUserPort);
                            try
                            {
                                client.Connect();
                                curClient = client;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        else
                        {
                            ChatClient client = new ChatClient(this, curUserIP, curUserPort);
                            if (client != curClient)
                            {
                                curClient.disConnect();
                                try
                                {
                                    client.Connect();
                                    curClient = client;
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }
                         //
                        curClient.SendMessage(txtMsgSend.Text);
                    }

              1using System;
              2using System.Collections.Generic;
              3using System.Text;
              4using System.Net;
              5using System.Net.Sockets;
              6using System.Threading;
              7using System.IO;
              8using System.Windows.Forms;
              9
             10namespace BurEmluator
             11{
             12    class ChatServer
             13    {
             14        public MutualWnd mwnd;
             15        public TcpListener listener;
             16        public TcpClient client;
             17        public String recMsg;
             18        Thread serverThread;
             19        ClientMessageReader reader; 
             20        public ChatServer(MutualWnd mwnd)
             21        {
             22            this.mwnd = mwnd;
             23        }

             24     
             25        public void Start()
             26        {
             27            String addIP = PublicUse.GetLocalIP();
             28            listener = new TcpListener(IPAddress.Parse(addIP), 8088);
             29            serverThread = new Thread(new ThreadStart(Listen));
             30            serverThread.Start();
             31        }

             32        public void Listen()
             33        {
             34            listener.Start();
             35            while (true)
             36            {
             37                client = listener.AcceptTcpClient();
             38                reader = new ClientMessageReader();
             39                reader.Stream = client.GetStream();
             40                reader.StartRead();
             41                recMsg = reader.res;
             42                if (mwnd.txtRecMsg.Text.Length > 0)
             43                {
             44                    mwnd.txtRecMsg.Text = mwnd.txtRecMsg.Text + "\r\n" + recMsg;
             45                }

             46                else
             47                {
             48                    mwnd.txtRecMsg.Text = recMsg;
             49                }

             50            }

             51        }

             52        public void close()
             53        {
             54            try
             55            {
             56                if (listener != null)
             57                {
             58                    listener.Stop();
             59                }

             60                if (client != null)
             61                {
             62                    client.Close();
             63                }

             64            }

             65            catch (Exception ex)
             66            {
             67                MessageBox.Show(ex.Message);
             68            }

             69            reader.CloseRead();
             70            try
             71            {
             72                if (serverThread != null)
             73                {
             74                    if (serverThread.IsAlive)
             75                    {
             76                        serverThread.Abort();
             77                    }

             78                }

             79              
             80            }

             81            catch (Exception ex)
             82            {
             83                MessageBox.Show(ex.Message);
             84            }

             85        }

             86
             87    }

             88    class ClientMessageReader
             89    {
             90        public NetworkStream Stream;
             91        public String res = null;
             92        protected StreamReader sr;
             93        Thread readThread; 
             94        public void StartRead()
             95        {
             96            readThread = new Thread(new ThreadStart(Read));
             97            readThread.Start();
             98        }

             99        public void CloseRead()
            100        {
            101            try
            102            {
            103                if (readThread != null)
            104                {
            105                    if (readThread.IsAlive)
            106                    {
            107                        readThread.Abort();
            108                    }

            109                }

            110            }

            111            catch(Exception ex)
            112            {
            113                MessageBox.Show(ex.Message);
            114            }

            115        }

            116        public void Read()
            117        {
            118
            119            sr = new StreamReader(Stream, System.Text.Encoding.Default);
            120            while (true)
            121            {
            122                try
            123                {
            124                    string line = sr.ReadLine();
            125                    if (line != null)
            126                    {
            127                        //System.Diagnostics.Debug.WriteLine(line);
            128                        res = line;
            129                    }

            130                }

            131                catch (Exception ex)
            132                {
            133                    System.Diagnostics.Debug.WriteLine(ex.Message + ":" + ex.StackTrace.ToString());
            134                }

            135                Thread.Sleep(100);
            136                Application.DoEvents();   
            137            }

            138        }

            139    }

            140}

            141

             1using System;
             2using System.Collections.Generic;
             3using System.Text;
             4using System.Net;
             5using System.Net.Sockets;
             6using System.Threading;
             7using System.IO;
             8using System.Windows.Forms;
             9
            10namespace BurEmluator
            11{
            12    class ChatClient
            13    {
            14        StreamWriter sw;
            15        NetworkStream stream;
            16        TcpClient client;
            17        MutualWnd mwnd;
            18        private string strIP;
            19        private int port;
            20        public ChatClient(MutualWnd mw, string strIP, int port)
            21        {
            22            this.mwnd = mw;
            23            this.strIP = strIP;
            24            this.port = port;
            25        }

            26        public bool Connect()
            27        {
            28            client = new TcpClient();
            29            try
            30            {
            31                client.Connect(IPAddress.Parse(strIP), port);
            32            }

            33            catch (Exception ex)
            34            {
            35                MessageBox.Show(ex.Message);
            36            }

            37            try
            38            {
            39                stream = client.GetStream();
            40                sw = new StreamWriter(stream, System.Text.Encoding.Default);
            41            }

            42            catch(Exception ex)
            43            {
            44                MessageBox.Show(ex.Message);
            45            }

            46            return true;
            47        }

            48        public bool disConnect()
            49        {
            50            if (sw != null)
            51            {
            52                sw.Close();
            53            }

            54            if (client != null)
            55            {
            56                client.Close();
            57            }

            58            return true;
            59        }

            60        public bool SendMessage(string strMessage)
            61        {
            62            if (strMessage != null && strMessage.Length > 0)
            63            {
            64                string info = "客戶端:" + PublicUse.GetLocalIP() + " 說 :" + "        (" + DateTime.Now.ToString() + ")" + "\r\n" + strMessage;
            65                sw.WriteLine(info);
            66                sw.Flush();//把消息推過去
            67                mwnd.txtMsgSend.Clear();
            68                if (mwnd.txtRecMsg.Text.Length > 0)
            69                {
            70                    mwnd.txtRecMsg.Text = mwnd.txtRecMsg.Text + "\r\n" + info;//同時(shí)顯示在自己這邊的txtRecMsg中
            71                }

            72                else
            73                {
            74                    mwnd.txtRecMsg.Text = info;
            75                }

            76            }

            77            else
            78            {
            79                MessageBox.Show("不能發(fā)送空消息");
            80            }

            81            return true;
            82        }

            83    }

            84}

            85
            posted on 2008-11-08 10:53 天書 閱讀(827) 評論(0)  編輯 收藏 引用

            <2009年2月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            1234567

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产精久久久久久久| 精品久久久无码人妻中文字幕 | 久久亚洲AV无码西西人体| 国产精品久久永久免费| 久久亚洲AV成人无码国产| 伊人久久大香线蕉综合影院首页| 亚洲精品NV久久久久久久久久| 久久国产精品免费一区二区三区| 久久国产乱子伦精品免费强| 72种姿势欧美久久久久大黄蕉| 国产亚洲色婷婷久久99精品| www.久久热.com| 97精品伊人久久久大香线蕉 | 久久不射电影网| 国产精品综合久久第一页| 久久99国产一区二区三区| 人妻丰满?V无码久久不卡| 久久久无码精品亚洲日韩京东传媒| 亚洲va久久久久| 久久婷婷国产综合精品 | 欧美午夜精品久久久久久浪潮| 人妻中文久久久久| 亚洲AV无码成人网站久久精品大| 久久水蜜桃亚洲av无码精品麻豆| 久久国产一区二区| 久久久91人妻无码精品蜜桃HD| 亚洲欧美日韩精品久久亚洲区| 无码AV中文字幕久久专区| 热久久这里只有精品| 天堂无码久久综合东京热| 国产精品一区二区久久国产| 99久久亚洲综合精品网站| 伊人热热久久原色播放www| 国产亚洲欧美精品久久久| 久久久精品国产亚洲成人满18免费网站 | 亚洲愉拍99热成人精品热久久| 欧美一区二区精品久久| 久久久久久伊人高潮影院| 91精品国产91久久| 日韩乱码人妻无码中文字幕久久| 久久国产精品视频|