TCP c/s模式实现点对点,一对多聊天 识别不同的TCP通道.给相应的TCP客户发送信息

客户端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace mytcpchat
{
    public partial class Form1 : Form
    {
        private Thread th;
        Socket c;
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }
         public void Client()
        {
            try
            {
                IPEndPoint ServerIPEP = new IPEndPoint(IPAddress.Parse(this.comboBox1.Text.Trim()), 5656);
                c = new Socket(ServerIPEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                c.Connect((EndPoint)ServerIPEP);
                c.Send(System.Text.Encoding.Default.GetBytes(this.sendmessage.Text.Trim()));
                byte[] data = new byte[2048];
                while (true)
                {
                    int rect = c.Receive(data);
                    byte[] chat = new byte[rect];
                    Buffer.BlockCopy(data, 0, chat, 0, rect);
                    this.message.AppendText(System.Text.Encoding.Default.GetString(chat));
                }
           }
            catch (Exception ex)
            {
              //  MessageBox.Show(ex.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
                    {
                        try
                        {

                            th = new Thread(new ThreadStart(Client));//新建一个用于监听的线程
                            th.Start();//打开新线程
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
        }
    }

服务端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Collections;
namespace mytcpserver
{
    public partial class Form1 : Form
    {
        Socket s;
        IPEndPoint ServerIPEP;
        private Thread th;
        Socket uc;
        private ArrayList alSock;//
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
         public void  Server()
        {
            int port = 5656;
            
            ServerIPEP = new IPEndPoint(IPAddress.Any, port);
            s = new Socket(ServerIPEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            s.Bind((EndPoint)ServerIPEP);
            s.Listen(10);
            alSock = new ArrayList();
            while (true)
            {
                try
                {
                    uc = s.Accept();
                    alSock.Add(uc);
                    this.textBox1.AppendText(System.Convert.ToString(uc));
                    byte[] data = new byte[2048];
                    int rect = uc.Receive(data);
                    byte[] chat = new byte[rect];
                    Buffer.BlockCopy(data, 0, chat, 0, rect);
                    this.mymessage.AppendText("["+uc.RemoteEndPoint.ToString() +"]"+ System.Text.Encoding.Default.GetString(chat));
                             
                }
                catch (Exception ex)
                {
                 //   MessageBox.Show(ex.Message);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            this.button2.Enabled = true;
            th = new Thread(new ThreadStart(Server));//新建一个用于监听的线程
            th.Start();//打开新线程
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.button2.Enabled = false;
                this.button1.Enabled = true;
                s.Close();
                th.Abort();//终止线程
                
            

            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (uc == null)
            {
                MessageBox.Show("你不能和任何人进行聊天,请告诉服务器进行聊天请求");
            }
            else
            {
                int index = int.Parse(this.textBox2.Text.Trim());
                Socket sc = (Socket)alSock[index];
                //发送数据
              sc.Send(System.Text.Encoding.Default.GetBytes(this.answermessage.Text.Trim()));
            }

        }
    }
}

posted @ 2010-03-29 12:55  与时俱进  阅读(1120)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈