资源简介
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 = “127.0.0.1“;
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)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3799 2011-08-08 00:29 AsyncTcp\AsyncTcpClient\AsyncTcpClient.csproj
文件 917 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\AsyncTcpClient.sln
..A..H. 13312 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\AsyncTcpClient.suo
文件 15872 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\bin\Debug\AsyncTcpClient.exe
文件 28160 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\bin\Debug\AsyncTcpClient.pdb
文件 14328 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe
文件 490 2007-07-21 01:33 AsyncTcp\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe.manifest
文件 11241 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\FormClient.cs
文件 8302 2011-08-08 01:14 AsyncTcp\AsyncTcpClient\FormClient.Designer.cs
文件 5814 2011-08-08 01:14 AsyncTcp\AsyncTcpClient\FormClient.resx
文件 1308 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.FileListAbsolute.txt
文件 852 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.GenerateResource.Cache
文件 15872 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.exe
文件 180 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.FormClient.resources
文件 28160 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.pdb
文件 180 2011-08-11 14:20 AsyncTcp\AsyncTcpClient\obj\Debug\AsyncTcpClient.Properties.Resources.resources
文件 500 2011-08-08 00:15 AsyncTcp\AsyncTcpClient\Program.cs
文件 1378 2011-08-08 00:03 AsyncTcp\AsyncTcpClient\Properties\AssemblyInfo.cs
文件 2878 2011-08-08 00:03 AsyncTcp\AsyncTcpClient\Properties\Resources.Designer.cs
文件 5612 2011-08-08 00:03 AsyncTcp\AsyncTcpClient\Properties\Resources.resx
文件 1099 2011-08-08 00:03 AsyncTcp\AsyncTcpClient\Properties\Settings.Designer.cs
文件 249 2011-08-08 00:03 AsyncTcp\AsyncTcpClient\Properties\Settings.settings
文件 3834 2011-08-07 19:33 AsyncTcp\AsyncTcpServer\AsyncTcpServer.csproj
文件 917 2011-08-11 14:20 AsyncTcp\AsyncTcpServer\AsyncTcpServer.sln
..A..H. 13312 2011-08-11 14:24 AsyncTcp\AsyncTcpServer\AsyncTcpServer.suo
文件 14848 2011-08-11 14:20 AsyncTcp\AsyncTcpServer\bin\Debug\AsyncTcpServer.exe
文件 30208 2011-08-11 14:20 AsyncTcp\AsyncTcpServer\bin\Debug\AsyncTcpServer.pdb
文件 14328 2011-08-11 14:45 AsyncTcp\AsyncTcpServer\bin\Debug\AsyncTcpServer.vshost.exe
文件 490 2007-07-21 01:33 AsyncTcp\AsyncTcpServer\bin\Debug\AsyncTcpServer.vshost.exe.manifest
文件 11080 2011-08-11 14:19 AsyncTcp\AsyncTcpServer\FormServer.cs
............此处省略35个文件信息
相关资源
- C#连接Oracle含源码和数据库
- vb.net 获取USB 摄像头 图像 Aforge方法
- C#如何实现图片查看器
- c#编写的简单运算器 含有代码 ,实现
- 金税接口调用
- 短路计算程序c#版
- 用C#编写的完整的CAD源码对于初学者很
- C#版医院门诊管理系统完整版附带数据
- Huber稳健估计
- C#调用金蝶登录窗口专业版12.2以后也
- C# 界面实现快速排序
- C#宾馆客房管理系统课设
- C# wpf 操作word Excel,
- 数学模拟钉板实验c#源码
- C# 图像处理软件及其源码
- C# DESC 加密解密
- 串口扫描枪 c# 最简程序源码
- 在C#中调用MapX[超级详细]pb、vb、delp
- c#操作文件实现日志功能
- 我用C#做的电子名片系统(带图片功能
- 操作系统 进程管理 C# VS2005
- ashxC#语言
- C#实现《CATIA二次开发技术基础》中的
- 文档管理系统源代码+数据库
- 人力资源管理系统源代码+数据库
- 学生管理系统传c#论文及源码
- C# 液晶控件 lcd控件 LCM控件 LED控件
- C#基本函数代码C#基本函数代码C#基本
- C# 自定义控件 自定义ComboBox
- C# 2015最新版NMODBUS4.0源码支持TCP/IPRT
评论
共有 条评论