资源简介
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个文件信息
相关资源
- C#中读取Excel表格数据
- Winform 画多边形
- 中兴华为C#编码规范
- C# winform Socket 即时通讯
- C#textbox下拉提示 textbox智能提示 te
- C#连接oracle数据库增删改查
- 基于GPRS的远程通讯C#程序SOCKET部分带
- C# winform 单击与双击事件同时存在
- c#+vs2005的局域网抢答器
- 留言板C# 窗口版和网页版
- C#:ARGB与16进制颜色显示与转换
- c#调用winapi实现WinForm中嵌入EXE程序
- otsu阈值分割c#语言
- C#和PLC利用modbus通信
- 百度翻译API,C#接口。
- 我写的TreeGrid源代码 TreeGrid 网格树录
- C#软键盘支持中英文输入
- BarScan条形码扫描源码c#
- 使用C#.NET实现FTP文件
- C# winform 朗读输入文本
- 电子邮件email收取客户端程序,C#源代
- HIKVISION工业相机通过Halcon二次开发的
- 用C#编程调用Visio的COM接口,获取Vis
- C#打印--打印商品出库单(亲测可用)
- C#实现滚动字幕完整源码(亲测可用)
- C# SqlHelper类
- 测绘编程技术——C#控制点展绘
- C#实现计算器
- C# 实现RDP远程桌面连接配套项目源码
- winform登陆界面展示
评论
共有 条评论