资源简介
史上最完整的的基于Winform下的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;
using System.Net.Sockets;
using System.Threading;
namespace Socket_Client_asynchronous
{
public partial class Form1 : Form
{
public TcpClient tcpc; //对服务器端建立TCP连接
public Socket tcpsend; //发送创建套接字
public bool connect_flag = false;
public byte[] receive_buff = new byte[4096];
public ManualResetEvent connectDone = new ManualResetEvent(false); //连接的信号
public ManualResetEvent readDone = new ManualResetEvent(false); //读信号
public ManualResetEvent sendDone = new ManualResetEvent(false); //发送结束
public bool connect(string address)
{
try
{
tcpsend = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);//初始化套接字
IPEndPoint remotepoint = new IPEndPoint(IPAddress.Parse(address) Convert.ToInt32(textBox3.Text));//根据ip地址和端口号创建远程终结点
EndPoint end = (EndPoint)remotepoint;
tcpsend.BeginConnect(end new AsyncCallback(ConnectedCallback) tcpsend); //调用回调函数
connectDone.WaitOne();
return true;
}
catch
{
return false;
}
}
private void ConnectedCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
connect_flag = true;
connectDone.Set();
}
public void send(string data)
{
int length = data.Length;
Byte[] Bysend = new byte[length];
Bysend = System.Text.Encoding.GetEncoding(“GB2312“).GetBytes(data); //将字符串指定到指定Byte数组
tcpsend.BeginSend(Bysend 0 Bysend.Length 0 new AsyncCallback(SendCallback) tcpsend); //异步发送数据
sendDone.WaitOne();
}
private void SendCallback(IAsyncResult ar) //发送的回调函数
{
Socket client = (Socket)ar.AsyncState;
int bytesSend = client.EndSend(ar); //完成发送
sendDone.Set();
}
public void receive() //接收数据
{
//byte[] receive=new byte[1024];
tcpsend.BeginReceive(receive_buff 0 receive_buff.Length 0 new AsyncCallback(ReceiveCallback) tcpsend);
//sendDone.WaitOne();
}
private void ReceiveCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState; //获取句柄
int bytesread = client.EndReceive(ar);
if (bytesread > 0)
{
tcpsend.BeginReceive(receive_buff 0 receive_buff.Length 0 new AsyncCallback(ReceiveCallback) client);
}
else
{
readDone.Se
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-03-01 23:57 Socket_asynchronous\
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\
文件 6411 2013-03-22 21:49 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.Designer.cs
文件 5302 2013-03-23 19:21 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.cs
文件 5817 2013-03-22 21:49 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Form1.resx
文件 507 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Program.cs
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\
文件 1384 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\AssemblyInfo.cs
文件 2902 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Resources.Designer.cs
文件 5612 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Resources.resx
文件 1111 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Settings.Designer.cs
文件 249 2013-03-01 16:15 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Properties\Settings.settings
文件 3711 2013-03-01 16:43 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous.csproj
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\
文件 11776 2013-03-23 19:21 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.exe
文件 38400 2013-03-23 19:21 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.pdb
文件 11600 2013-03-23 19:28 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.vshost.exe
文件 490 2009-08-31 00:40 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\bin\Debug\Socket_Client_asynchronous.vshost.exe.manifest
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\
目录 0 2013-03-01 23:57 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\
目录 0 2013-03-23 19:21 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\
文件 4440 2013-03-01 19:46 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6356 2013-03-23 19:21 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1296 2013-03-22 21:49 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\GenerateResource.read.1.tlog
文件 3118 2013-03-22 21:49 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\GenerateResource.write.1.tlog
文件 7897 2013-03-01 19:56 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\ResolveAssemblyReference.cache
文件 180 2013-03-22 21:49 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.Form1.resources
文件 180 2013-03-12 17:24 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.Properties.Resources.resources
文件 3720 2013-03-23 19:28 Socket_asynchronous\Socket_Client_asynchronous\Socket_Client_asynchronous\obj\x86\Debug\Socket_Client_asynchronous.csproj.FileListAbsolute.txt
............此处省略40个文件信息
相关资源
- .NET C#研发的授权工具winform
- 在winform下,利用控件ZedGraph控件绘制
- C#WinForm获取子窗口返回值
- C#Winform温度计控件
- winForm word转pdf excel转pdf
- Textbox背景透明(winform)
- c#+WINFORM+漂亮的软件启动界面特效源码
- C#winform实现钢琴7音
- C#WinForm使用CefSharp内嵌
- Winform可视化打印模板设计(完整版)
- zw_cwinform用鼠标画拉出来的虚线框鼠标
- Winform增删改查小项目带数据库
- WPF对比WinForm
- Winform自定义MessageBox
- C#银行ATM程序WinForm
- winform创建不规则窗体和控件源码.ra
- WCF大文件分段上传winform
- 在winform中播放gif动画图片
- WinForm连接SQLServer 一份C#小代码
- c#(winform)调用VBS脚本
- Winform不规则窗体
- winform窗体中嵌入网页(DHTML代码和w
- WinForm仿QQ截图功能
- c# winform wia 调用扫描仪
- c# icon 图标 特效 WINDOWS任务栏 动态生
- C# Winform 中实现的翻译功能
- winform系统托图标盘闪烁
- VS2013 WinForm ReportViewer 子报表 使用分组
- winform(c#) DataGridView控件多维合并表
- C#计算器堆栈版
评论
共有 条评论