• 大小: 95KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: C#
  • 标签: Socket  

资源简介

基于Socket的Winform聊天小程序,可实现多人在线聊天,包含客户端及服务端源码,代码简洁易懂,注释清晰,方便理解

资源截图

代码片段和文件信息

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace socketClient
{
    public partial class Form1 : Form
    {
        private int count = 0;
        public Form1()
        {
            InitializeComponent();
        }
        /// 
        /// tcp客户端
        /// 

        private TcpClient _client;

        /// 
        /// 窗体加载事件
        /// 

        private void Form1_Load(object sender EventArgs e)
        {
            //初始化tcp客户端
            _client = new TcpClient();
            try
            {
                _client.Connect(IPAddress.Parse(“111.230.240.198“) 10800);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(“未能连接到服务器 {0}“ ex.Message));
                this.Close();
                return;
            }

            //接收消息线程
            Thread reciveMessageThread = new Thread(ReciveMessage);
            reciveMessageThread.Start();
        }

        /// 
        /// 窗体关闭事件
        /// 

        private void Form1_FormClosing(object sender FormClosingEventArgs e)
        {
            _client.Close();
        }
        

        /// 
        /// 接收消息
        /// 

        private void ReciveMessage()
        {
            try
            {
                NetworkStream stream = _client.GetStream();
                while (true)
                {
                    byte[] data = new byte[1024];
                    int length = stream.Read(data 0 data.Length);
                    if (length > 0)
                    {
                        string msg = Encoding.UTF8.GetString(data 0 length);

                        //添加到前端消息列表
                        int num = count - lbx_Messages.Items.Count;
                        for (int i = 0; i < num; i++)
                        {
                            lbx_Messages.Items.Add(““);
                        }
                        lbx_Messages.Items.Add(msg);
                        count += 1;

                    }
                    else
                    {
                        MessageBox.Show(“服务器已关闭“);
                        stream.Dispose();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                //Read是阻塞方法 程序退出释放资源是会引发异常 不做处理 线程结束
            }
        }
        

        private void btn_Send_Click(object sender EventArgs e)
        {
            //发送消息至服务器
            string msg = tbx_Message.Text;
            byte[] data = Encoding.UTF8.GetBytes(msg);
            try
            {
                NetworkStream stream = _client.GetStream();
                stream.Write(data 0 data.Length);

                //添加到前端消息列表

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        189  2017-12-12 16:46  SocketDemo\socketClient\App.config

     文件      10752  2017-12-13 14:11  SocketDemo\socketClient\bin\Debug\socketClient.exe

     文件        189  2017-12-12 16:46  SocketDemo\socketClient\bin\Debug\socketClient.exe.config

     文件      22016  2017-12-13 14:11  SocketDemo\socketClient\bin\Debug\socketClient.pdb

     文件      22696  2017-12-13 16:11  SocketDemo\socketClient\bin\Debug\socketClient.vshost.exe

     文件        189  2017-12-12 16:46  SocketDemo\socketClient\bin\Debug\socketClient.vshost.exe.config

     文件        490  2017-03-19 05:00  SocketDemo\socketClient\bin\Debug\socketClient.vshost.exe.manifest

     文件       3665  2017-12-13 14:11  SocketDemo\socketClient\Form1.cs

     文件       5062  2017-12-12 18:08  SocketDemo\socketClient\Form1.Designer.cs

     文件       5817  2017-12-12 18:08  SocketDemo\socketClient\Form1.resx

     文件       1464  2017-12-13 11:31  SocketDemo\socketClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7080  2017-12-12 16:46  SocketDemo\socketClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        546  2017-12-13 16:11  SocketDemo\socketClient\obj\Debug\socketClient.csproj.FileListAbsolute.txt

     文件       1012  2017-12-13 14:11  SocketDemo\socketClient\obj\Debug\socketClient.csproj.GenerateResource.Cache

     文件       2384  2017-12-12 17:33  SocketDemo\socketClient\obj\Debug\socketClient.csprojResolveAssemblyReference.cache

     文件      10752  2017-12-13 14:11  SocketDemo\socketClient\obj\Debug\socketClient.exe

     文件        180  2017-12-13 14:11  SocketDemo\socketClient\obj\Debug\socketClient.Form1.resources

     文件      22016  2017-12-13 14:11  SocketDemo\socketClient\obj\Debug\socketClient.pdb

     文件        180  2017-12-12 16:55  SocketDemo\socketClient\obj\Debug\socketClient.Properties.Resources.resources

     文件          0  2017-12-12 16:46  SocketDemo\socketClient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-12-12 16:46  SocketDemo\socketClient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-12-12 16:46  SocketDemo\socketClient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        524  2017-12-12 16:46  SocketDemo\socketClient\Program.cs

     文件       1332  2017-12-12 16:46  SocketDemo\socketClient\Properties\AssemblyInfo.cs

     文件       2837  2017-12-12 16:46  SocketDemo\socketClient\Properties\Resources.Designer.cs

     文件       5612  2017-12-12 16:46  SocketDemo\socketClient\Properties\Resources.resx

     文件       1099  2017-12-12 16:46  SocketDemo\socketClient\Properties\Settings.Designer.cs

     文件        249  2017-12-12 16:46  SocketDemo\socketClient\Properties\Settings.settings

     文件       3922  2017-12-12 16:53  SocketDemo\socketClient\socketClient.csproj

     文件        189  2017-11-30 15:39  SocketDemo\socketServer\App.config

............此处省略50个文件信息

评论

共有 条评论