资源简介
初步完成了异步TCP客户端+掉线自动重连,刚开始接触socket编程,对委托、回调、异步冒似还是似懂非懂,希望大家多多指导。http://u.download.csdn.net/images/btn_submit.png
代码片段和文件信息
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;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace MulThreadAsTCP
{
public partial class MainForm : Form
{
private Socket socket;
//Thread threadConnect;
private int sendCount = 0;
private int receiveCount = 0;
/*------------定义与声明委托------------*/
//显示消息
private delegate void ShowMsgCallBack(string str);
private ShowMsgCallBack showMsgCallBack;
//显示状态
private delegate void ShowStatusInfoCallBack(string str);
private ShowStatusInfoCallBack showStatusInfoCallBack;
//显示发送计数
private delegate void ShowSendCountCallBack(string str);
private ShowSendCountCallBack showSendCountCallBack;
//显示接收计数
private delegate void ShowReceiveCountCallBack(string str);
private ShowReceiveCountCallBack showReceiveCountCallBack;
//定时重连
private delegate void ReConnectCallBack();
private ReConnectCallBack reConnectCallBack;
//异步调用(与要调用的方法具有相同签名)
private delegate void ReceiveMsgDelegate(out string receiveMsg);
private ReceiveMsgDelegate receiveMsgDelegate;
private delegate void SendMsgDelegate(string sendMsg);
private SendMsgDelegate sendMsgDelegate;
/*------------定义与声明委托------------*/
public MainForm()
{
InitializeComponent();
/*----------实例化委托----------*/
//显示消息
showMsgCallBack = new ShowMsgCallBack(ShowMsg);
//显示状态
showStatusInfoCallBack = new ShowStatusInfoCallBack(ShowStatusInfo);
//显示发送计数
showSendCountCallBack = new ShowSendCountCallBack(ShowSendCount);
//显示接收计数
showReceiveCountCallBack = new ShowReceiveCountCallBack(ShowReceiveCount);
//掉线后自动启动定时重连
reConnectCallBack = new ReConnectCallBack(ReConnect);
//接收消息
receiveMsgDelegate = new ReceiveMsgDelegate(AsyncRcvMsg);
//发送消息
sendMsgDelegate = new SendMsgDelegate(AsyncSendMsg);
/*----------实例化委托----------*/
IPAddress[] localIP = Dns.GetHostAddresses(““);
txtIP.Text = localIP[0].ToString();
txtIP.SelectAll();
txtPort.Text = “9501“;
tbxReceiveCount.Text = sendCount.ToString();
tbxReceiveCount.Text = receiveCount.ToString();
}
/*----------定义回调函数----------*/
//显示消息
private void ShowMsg(string str)
{
lstBoxMsg.Items.Add(str);
lstBoxMsg.TopIndex = lstBoxMsg.Items.Count - 1;
}
//显示状态
private void ShowStatusInfo(string str)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 16896 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\bin\Debug\MulThreadAsTCP.exe
文件 34304 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\bin\Debug\MulThreadAsTCP.pdb
文件 11600 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\bin\Debug\MulThreadAsTCP.vshost.exe
文件 490 2010-03-17 22:39 MulThreadAsTCP\MulThreadAsTCP\bin\Debug\MulThreadAsTCP.vshost.exe.manifest
文件 10916 2015-08-26 16:33 MulThreadAsTCP\MulThreadAsTCP\MainForm.cs
文件 11087 2015-08-26 16:33 MulThreadAsTCP\MulThreadAsTCP\MainForm.Designer.cs
文件 6020 2015-08-26 16:33 MulThreadAsTCP\MulThreadAsTCP\MainForm.resx
文件 3702 2015-08-25 15:55 MulThreadAsTCP\MulThreadAsTCP\MulThreadAsTCP.csproj
文件 4440 2015-08-26 07:55 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6432 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 534 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\GenerateResource.read.1.tlog
文件 1194 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\GenerateResource.write.1.tlog
文件 1372 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\MulThreadAsTCP.csproj.FileListAbsolute.txt
文件 16896 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\MulThreadAsTCP.exe
文件 180 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\MulThreadAsTCP.MainForm.resources
文件 34304 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\MulThreadAsTCP.pdb
文件 180 2015-08-25 16:30 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\MulThreadAsTCP.Properties.Resources.resources
文件 7897 2015-08-26 07:59 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\ResolveAssemblyReference.cache
文件 498 2015-08-25 15:42 MulThreadAsTCP\MulThreadAsTCP\Program.cs
文件 1384 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\Properties\AssemblyInfo.cs
文件 2878 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\Properties\Resources.Designer.cs
文件 5612 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\Properties\Resources.resx
文件 1099 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\Properties\Settings.Designer.cs
文件 249 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\Properties\Settings.settings
文件 884 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP.sln
..A..H. 24576 2015-08-26 16:33 MulThreadAsTCP\MulThreadAsTCP.suo
目录 0 2015-08-25 15:23 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug\TempPE
目录 0 2015-08-26 16:31 MulThreadAsTCP\MulThreadAsTCP\obj\x86\Debug
目录 0 2015-08-26 07:55 MulThreadAsTCP\MulThreadAsTCP\bin\Debug
目录 0 2015-08-25 15:42 MulThreadAsTCP\MulThreadAsTCP\bin\Release
............此处省略9个文件信息
- 上一篇:ModBus通信协议英文
- 下一篇:lims实验室信息管理系统
评论
共有 条评论