点对点的聊天

点对点的聊天:

namespace Chat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Thread listenthread;//表示监听的线程
        private string msg; //要发送的消息
        private Socket socket1; //Socket类用于接收和发送信息        
        private IPEndPoint lep;

        private void listenstart()//监听发入的信息
        {
            try
            {
                while (this.socket1.Connected)
                {
                    //设置一个缓冲区
                    Byte[] stream = new Byte[1024];
                    socket1.Receive(stream);
                    //将获得的流转化为字符串类型
                    string message = Encoding.Default.GetString(stream);
                    //this.textBox1.Text=socket1.Available.ToString();
                    this.richTextBox2.AppendText(message);
                    this.richTextBox2.AppendText(" ");
                }
            }
            catch//(Exception error)
            {
                if (!this.socket1.Connected)
                {
                    socket1.Close();
                    button3.Enabled = true;
                    button5.Enabled = true;
                }
            }
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            //获取本地主机名称
            string host = Dns.GetHostName();
            this.textBox1.Text = host;

            //获取本机IP
            IPAddress[] addrs = Dns.Resolve("localhost").AddressList;
            IPAddress addr = Dns.Resolve(host).AddressList[0];
            //转化为标准的以点分隔的四部分格式
            this.textBox2.Text = addr.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获得监听端口
            int hostport;
            try
            {
                hostport = Int32.Parse(this.textBox5.Text);
                if (hostport < 135 || hostport > 65400) hostport = 12800;
            }
            catch
            {
                hostport = 12800;
            }
            //IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
            IPAddress ipAddress = IPAddress.Parse("0.0.0.0");
            lep = new IPEndPoint(ipAddress, hostport);
            socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket1.Bind(lep);
                socket1.Listen(10000);
                this.statusBar1.Text = "等待连接...";
                button3.Enabled = false;
                button5.Enabled = false;
                Thread watt = new Thread(new ThreadStart(waits));
                watt.Start();

            }
            catch
            {
                button3.Enabled = true;
            }

        }
        private void waits()
        {
            try
            {
                socket1 = socket1.Accept();
                this.statusBar1.Text = "已建立连接";
                listenthread = new Thread(new ThreadStart(listenstart));
                listenthread.Start();
                button3.Enabled = false;
                button5.Enabled = false;
            }
            catch
            {
                button3.Enabled = true;
                button5.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             if(socket1.Connected)
            {
                //将发送的字符串转换成字节数组
                msg = "<"+this.textBox3.Text+">"+this.richTextBox1.Text;
                Byte[] bytes=Encoding.Default.GetBytes(msg);
                //发送数组
                try
                {
                    socket1.Send(bytes,bytes.Length,SocketFlags.None);
                    this.richTextBox1.Text="";
                }
                catch
                {
                }                
                //this.richTextBox2.AppendText(msg);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text = "";  
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获得连接端口
            int hostport;
            try
            {                    
                hostport=Int32.Parse(this.textBox6.Text);
                if(hostport<135||hostport>65400)hostport=12800;
            }
            catch
            {
                hostport=30000;
            }
            
            try
            {
                //得到需连接的地址
                lep = new IPEndPoint(IPAddress.Parse(this.textBox4.Text),hostport);                    
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);
            }
            //构造socket1实例,并请求连接
            socket1= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            try
            {
                socket1.Connect(lep);
                if(socket1.Connected)this.statusBar1.Text="连接成功";
                //开始监听
                listenthread=new Thread(new ThreadStart(listenstart));
                listenthread.Start();
                button5.Enabled=false;
                button3.Enabled=false;
            }
            catch
            {
                button5.Enabled=true;
                button3.Enabled=true;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                socket1.Close();
                if (this.listenthread!=null)
                {
                    //如果线程还处于运行状态就关闭它
                    if (this.listenthread.ThreadState==ThreadState.Running)
                    {
                        this.listenthread.Abort();
                    }
                }
            }
            catch
            {
            }
        }
    }
}

 

你可能感兴趣的:(thread,socket)