网络中的一对多通信

实现一对多的网络通信先收藏了。

//客户端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace TcpClient2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }

 

        private void button2_Click(object sender, EventArgs e)
        {
            //if (New_TB.Text.Trim().Length > 0)
            //{
            //    TcpClient TC = new TcpClient(IP_TB.Text,int.Parse(Point_TB.Text));
            //    NetworkStream NetStr = TC.GetStream();
            //    NetStr.Write(Encoding.UTF8.GetBytes(New_TB.Text), 0, Encoding.UTF8.GetBytes(New_TB.Text).Length);
            //    writestring("客户端说:" + New_TB.Text);
            //    New_TB.Clear();
            //    Byte[] newdate = new Byte[256];
            //    int len = NetStr.Read(newdate, 0, newdate.Length);
            //    writestring("服务端说:" + Encoding.UTF8.GetString(newdate));
            //    NetStr.Close();
            //    TC.Close();
            //}
            if (New_TB.Text.Trim().Length > 0)
            {
                TcpClient TC = new TcpClient(IP_TB.Text, int.Parse(Point_TB.Text));
                byte[] data = Encoding.UTF8.GetBytes(New_TB.Text);
                NetworkStream NetStr = TC.GetStream();
                NetStr.Write(data, 0, data.Length);
                writestring("客户端说:" + New_TB.Text);
                New_TB.Clear();
                Byte[] newdate = new Byte[256];
                int len = NetStr.Read(newdate, 0, newdate.Length);
                writestring("服务端说:" + Encoding.UTF8.GetString(newdate));
                NetStr.Close();
                TC.Close();
            }

        }
        void writestring(string s)
        {
            Old_TB.Text += DateTime.Now.ToString("yy/MM/dd hh:mm:ss") + s;
            Old_TB.Text += "/r/n";
        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

    }
}

//服务器端

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

namespace TCPClient1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread ServerTh = new Thread(ServerListener);
            ServerTh.Start();
        }
        //void ServerListener()
        //{
        //    IPAddress Ip = IPAddress.Parse(IP_TB.Text);
        //    TcpListener server = new TcpListener(Ip,int.Parse(Point_TB.Text));
        //    server.Start();
        //    Byte[] bytes=new Byte[256];
        //    String data = null;
        //    while (true)
        //    {
        //        TcpClient client = server.AcceptTcpClient();
        //        NetworkStream NetStr = client.GetStream();
        //        int i = 0;
        //        while ((i = NetStr.Read(bytes, 0, bytes.Length)) != 0)
        //        {
        //            data = System.Text.Encoding.UTF8.GetString(bytes);
        //            writestring("客户端说:" + data);
        //            data = New_TB.Text;
        //            byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
        //            NetStr.Write(msg, 0, msg.Length);
        //            writestring("服务端说:" + data);

        //        }
        //    }
        //}

        List<TcpClient> list = new List<TcpClient>();
        void ServerListener()
        {
            IPAddress IP = IPAddress.Parse(IP_TB.Text);
            TcpListener server = new TcpListener(IP, int.Parse(Point_TB.Text));
            server.Start();
            Byte[] bytes = new Byte[256];
            String data = null;
            while (true)
            {
                TcpClient client = server.AcceptTcpClient();

                NetworkStream NetStr = client.GetStream();

                int i = 0;
                while ((i = NetStr.Read(bytes, 0, bytes.Length)) != 0)
                {
                    data = System.Text.Encoding.UTF8.GetString(bytes);
                    writestring("客户端说:" + data);
                    data = New_TB.Text;
                    byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
                    NetStr.Write(msg, 0, msg.Length);
                    writestring("服务端说:" + data); ;
                }
            }

        }
        void writestring(string s)
        {
            Old_TB.Text += DateTime.Now.ToString("yy/MM/dd hh:mm:ss") + s;
            Old_TB.Text += "/r/n";
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值