zoukankan      html  css  js  c++  java
  • 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()));
                }

            }
        }
    }

  • 相关阅读:
    Linux 改变文件的所有者
    Opencv -lippicv
    数据结构--二叉搜索树
    Window下cmd查看目录结构
    windows cmake与nmake
    Ubuntu18.04安装caffe python3.6 opencv3.2 CPU
    apt-get install 下载速度慢问题的解决
    使用pip安装速度慢问题的解决
    神经网络可视化
    【VS】代码行无法折叠及ctrl+鼠标左键无法跳转到定义的问题
  • 原文地址:https://www.cnblogs.com/top5/p/1699491.html
Copyright © 2011-2022 走看看