• <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) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品国产亚洲av瑜伽| 久久精品aⅴ无码中文字字幕重口| 久久久久久A亚洲欧洲AV冫| 三级三级久久三级久久| 国产精品久久久久久久久免费| 久久这里有精品| 国产福利电影一区二区三区,免费久久久久久久精 | 伊人久久大香线蕉AV色婷婷色| 91精品国产综合久久香蕉| 人妻无码αv中文字幕久久琪琪布| 久久天堂电影网| 久久夜色精品国产噜噜麻豆| 欧美日韩中文字幕久久久不卡| 久久综合久久综合久久| 亚洲国产另类久久久精品黑人| 青青草原综合久久大伊人导航| 久久97久久97精品免视看秋霞| 99久久久国产精品免费无卡顿| 久久人人爽人人爽人人片AV东京热 | 麻豆国内精品久久久久久| 2021少妇久久久久久久久久| 精品国产乱码久久久久久人妻 | 99热热久久这里只有精品68| 亚洲va中文字幕无码久久不卡| 亚洲午夜精品久久久久久app| 久久精品国产72国产精福利| 久久91精品国产91久久麻豆| 久久人人爽爽爽人久久久| 综合人妻久久一区二区精品| 久久无码AV中文出轨人妻| 狠狠色丁香婷婷久久综合| 久久夜色精品国产亚洲av| 国产精品成人99久久久久91gav| 久久精品国产秦先生| av无码久久久久不卡免费网站 | 怡红院日本一道日本久久 | 亚洲国产日韩综合久久精品| 久久亚洲2019中文字幕| 香蕉久久夜色精品国产尤物| 久久久无码精品午夜| 亚洲а∨天堂久久精品|