posts - 124,  comments - 29,  trackbacks - 0

调用过程:
//初始化  始终开着服务器端进程监听有没有发消息过来的客户      
private void MutualWnd_Load(object sender, EventArgs e)
        {
            #region 开启服务端
            CheckForIllegalCrossThreadCalls = false;
            server = new ChatServer(this);
            server.Start();
            #endregion
        }

//发送
        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;//同时显示在自己这边的txtRecMsg中
71                }

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

76            }

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

81            return true;
82        }

83    }

84}

85
posted on 2008-11-08 10:53 天书 阅读(811) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



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

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜