• 大小: 90KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: C#
  • 标签: C#  socket  聊天  

资源简介

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.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Threading;

namespace AsyncTcpClient
{
    public partial class FormClient : Form
    {
        //是否正常退出
        private bool isExit = false;
        private TcpClient client;
        private BinaryReader br;
        private BinaryWriter bw;
        BackgroundWorker connectWork = new BackgroundWorker();
        private string serverIP = “192.168.1.224“;
        public FormClient()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            Random r = new Random((int)DateTime.Now.Ticks);
            txt_UserName.Text = “user“ + r.Next(100 999);
            lst_OnlineUser.HorizontalScrollbar = true;
            connectWork.DoWork += new DoWorkEventHandler(connectWork_DoWork);
            connectWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(connectWork_RunWorkerCompleted);
        }

        /// 
        /// 异步方式与服务器进行连接
        /// 

        /// 
        /// 
        void connectWork_DoWork(object sender DoWorkEventArgs e)
        {
            client = new TcpClient();
            IAsyncResult result = client.BeginConnect(serverIP 8889 null null);
            while (!result.IsCompleted)
            {
                Thread.Sleep(100);
                AddStatus(“.“);
            }
            try
            {
                client.EndConnect(result);
                e.Result = “success“;
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
                return;
            }
        }

        /// 
        /// 异步方式与服务器完成连接操作后的处理
        /// 

        /// 
        /// 
        void connectWork_RunWorkerCompleted(object sender RunWorkerCompletedEventArgs e)
        {
            if (e.Result.ToString() == “success“)
            {
                AddStatus(“连接成功“);
                //获取网络流
                NetworkStream networkStream = client.GetStream();
                //将网络流作为二进制读写对象
                br = new BinaryReader(networkStream);
                bw = new BinaryWriter(networkStream);
                AsyncSendMessage(“Login“ + txt_UserName.Text);
                Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
                threadReceive.IsBackground = true;
                threadReceive.Start();
            }
            else
            {
                AddStatus(“连接失败:“ + e.Result);
                btn_Login.Enabled = true;
            }
        }

        private void btn_Login_Click(object sender EventArgs e)

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

     文件       5612  2011-08-07 19:16  AsyncTcpServer\Properties\Resources.resx

     文件        249  2011-08-07 19:16  AsyncTcpServer\Properties\Settings.settings

     文件       1378  2011-08-07 19:16  AsyncTcpServer\Properties\AssemblyInfo.cs

     文件       2878  2011-08-07 19:16  AsyncTcpServer\Properties\Resources.Designer.cs

     文件       1099  2011-08-07 19:16  AsyncTcpServer\Properties\Settings.Designer.cs

     文件      11084  2011-08-08 02:09  AsyncTcpServer\FormServer.cs

     文件        804  2011-08-07 19:22  AsyncTcpServer\User.cs

     文件       3834  2011-08-07 19:33  AsyncTcpServer\AsyncTcpServer.csproj

     文件      14848  2011-08-08 02:09  AsyncTcpServer\bin\Debug\AsyncTcpServer.exe

     文件      34304  2011-08-08 02:09  AsyncTcpServer\bin\Debug\AsyncTcpServer.pdb

     文件      14328  2011-08-08 02:09  AsyncTcpServer\bin\Debug\AsyncTcpServer.vshost.exe

     文件        180  2011-08-08 02:09  AsyncTcpServer\obj\Debug\AsyncTcpServer.FormServer.resources

     文件        180  2011-08-07 19:33  AsyncTcpServer\obj\Debug\AsyncTcpServer.Properties.Resources.resources

     文件        706  2011-08-08 02:09  AsyncTcpServer\obj\Debug\AsyncTcpServer.csproj.FileListAbsolute.txt

     文件      34304  2011-08-08 02:09  AsyncTcpServer\obj\Debug\AsyncTcpServer.pdb

     文件      14848  2011-08-08 02:09  AsyncTcpServer\obj\Debug\AsyncTcpServer.exe

     文件        852  2011-08-08 02:09  AsyncTcpServer\obj\Debug\AsyncTcpServer.csproj.GenerateResource.Cache

     文件       4146  2011-08-08 02:09  AsyncTcpServer\FormServer.Designer.cs

     文件       5814  2011-08-08 02:09  AsyncTcpServer\FormServer.resx

     文件        500  2011-08-07 19:16  AsyncTcpServer\Program.cs

     文件       5612  2011-08-08 00:03  AsyncTcpClient\Properties\Resources.resx

     文件        249  2011-08-08 00:03  AsyncTcpClient\Properties\Settings.settings

     文件       1378  2011-08-08 00:03  AsyncTcpClient\Properties\AssemblyInfo.cs

     文件       2878  2011-08-08 00:03  AsyncTcpClient\Properties\Resources.Designer.cs

     文件       1099  2011-08-08 00:03  AsyncTcpClient\Properties\Settings.Designer.cs

     文件       8302  2011-08-08 01:14  AsyncTcpClient\FormClient.Designer.cs

     文件       5814  2011-08-08 01:14  AsyncTcpClient\FormClient.resx

     文件       3799  2011-08-08 00:29  AsyncTcpClient\AsyncTcpClient.csproj

     文件      15872  2011-08-08 01:55  AsyncTcpClient\bin\Debug\AsyncTcpClient.exe

     文件      32256  2011-08-08 01:55  AsyncTcpClient\bin\Debug\AsyncTcpClient.pdb

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

评论

共有 条评论