• 大小: 11.23MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-06-19
  • 语言: C#
  • 标签: C#Soc  多人聊天  

资源简介

C#Socket网络通信实现多人聊天

资源截图

代码片段和文件信息

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


namespace Client_客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket mySocket_用于连接发送;
        Socket mySocket_接收;
        IPAddress Broadcast;
        IPEndPoint ep;
        UdpClient myUdpClient;
        Socket mycij;
        string  自己ID;
        void Add(string a)
        {
            txt1.AppendText(a+“\r\n“);
            txt1.ScrollToCaret();
        }
        private void Form1_Load(object sender EventArgs e)
        {

          
        }
        string  提取(byte[] mybyteref int ID)
        {
            int k = 0;
            for (int i = 0; i < mybyte.Length; i++)
            {
                if (mybyte[i] == 64)
                {
                    k = i;
                    break;
                }
            }
            string str = Encoding.ASCII.GetString(mybyte 0 k);
            ID = Convert.ToInt32(str);
            string s = Encoding.ASCII.GetString(mybytek+1 mybyte.Length-k-1);
            return s;
        }
        void Send()
        {
            try
            {
                Byte[] sendbuf = Encoding.GetEncoding(“gb2312“).GetBytes(“1“+自己ID.ToString()+“@“+txt2.Text.Trim().ToString());
                mySocket_用于连接发送.SendTo(sendbuf ep);
            }catch(Exception e)
            {
                Add(e.ToString());
            }
           
           
        }
        void 接收数据()
        {
            do
            {
                try
                {
                    byte[] b = new byte[100];
                    int 长度= mySocket_接收.Receive(b);
                    string str = Encoding.UTF8.GetString(b0 长度);
                    string   ID = ““;
                    string 消息 = 提取(str ref ID);
                  
                    if (消息 == “0“)
                    {
                        if(ID == 自己ID)
                        {
                            Add(“接入成功“);
                        }
                        else
                        {
                            Add(ID + “进入房间“);
                        }
                    }
                    else { if(ID == 自己ID)
                        {
                            Add( “我说:“ + 消息);
                        }
                        else
                        {
                            Add(ID + “说:“ + 消息);
                        }
                             }
                    
                }
                catch
                {

                }


            } while (true);
        }
        private void button1_Click(object sende

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-08-03 10:18  Client-客户端\
     目录           0  2016-08-03 10:18  Client-客户端\.vs\
     目录           0  2016-08-03 10:18  Client-客户端\.vs\Client-客户端\
     目录           0  2016-08-03 10:18  Client-客户端\.vs\Client-客户端\v14\
     文件       34816  2017-07-19 15:05  Client-客户端\.vs\Client-客户端\v14\.suo
     目录           0  2017-05-22 08:16  Client-客户端\Client-客户端\
     文件        1015  2016-04-18 12:47  Client-客户端\Client-客户端.sln
     文件         184  2017-05-22 08:15  Client-客户端\Client-客户端\App.config
     目录           0  2016-08-03 10:18  Client-客户端\Client-客户端\bin\
     目录           0  2017-05-22 08:13  Client-客户端\Client-客户端\bin\Debug\
     文件       12800  2017-05-22 08:16  Client-客户端\Client-客户端\bin\Debug\Client-客户端.exe
     文件         184  2017-05-22 08:15  Client-客户端\Client-客户端\bin\Debug\Client-客户端.exe.config
     文件       26112  2017-05-22 08:16  Client-客户端\Client-客户端\bin\Debug\Client-客户端.pdb
     文件       22688  2017-07-19 15:04  Client-客户端\Client-客户端\bin\Debug\Client-客户端.vshost.exe
     文件         184  2017-05-22 08:15  Client-客户端\Client-客户端\bin\Debug\Client-客户端.vshost.exe.config
     文件         490  2017-03-19 05:00  Client-客户端\Client-客户端\bin\Debug\Client-客户端.vshost.exe.manifest
     目录           0  2016-04-18 12:47  Client-客户端\Client-客户端\bin\Release\
     文件        3997  2017-05-22 08:15  Client-客户端\Client-客户端\Client-客户端.csproj
     文件        5817  2017-05-22 08:16  Client-客户端\Client-客户端\Form1.cs
     文件        8463  2016-06-09 12:49  Client-客户端\Client-客户端\Form1.Designer.cs
     文件        5817  2016-06-09 12:49  Client-客户端\Client-客户端\Form1.resx
     目录           0  2016-08-03 10:18  Client-客户端\Client-客户端\obj\
     目录           0  2017-05-22 08:15  Client-客户端\Client-客户端\obj\Debug\
     文件        2496  2017-07-19 15:04  Client-客户端\Client-客户端\obj\Debug\Client-客户端.csproj.FileListAbsolute.txt
     文件         976  2017-05-22 08:13  Client-客户端\Client-客户端\obj\Debug\Client-客户端.csproj.GenerateResource.Cache
     文件        8563  2017-05-22 08:15  Client-客户端\Client-客户端\obj\Debug\Client-客户端.csprojResolveAssemblyReference.cache
     文件       12800  2017-05-22 08:16  Client-客户端\Client-客户端\obj\Debug\Client-客户端.exe
     文件       26112  2017-05-22 08:16  Client-客户端\Client-客户端\obj\Debug\Client-客户端.pdb
     文件         180  2017-05-22 08:15  Client-客户端\Client-客户端\obj\Debug\Client_客户端.Form1.resources
     文件         180  2017-05-22 08:15  Client-客户端\Client-客户端\obj\Debug\Client_客户端.Properties.Resources.resources
     文件        1760  2017-05-22 08:15  Client-客户端\Client-客户端\obj\Debug\DesignTimeResolveAssemblyReferences.cache
............此处省略66个文件信息

评论

共有 条评论