设为首页
加入收藏
站内地图
旧版入口
首 页
新闻资讯
SEO研究
SEO查询
网站运营
站长学院
赚钱联盟
链接交换
下载中心
搜优客论坛
网页制作
网络编程
数据库
图形图像
操作系统
网络应用
当前位置:
首页
>
站长学院
>
网络编程
>
ASP.NET
Socket异步编程-之客户器端
作者:
佚名
出处:
网络转载
时间:
08-10
点击:
内容载入中...
教程中国版权声明: 吴龙杰
1
using
System;
2
using
System.Text;
3
using
System.Net;
4
using
System.Net.Sockets;
5
namespace
asyncSocketClient {
6
class
SocketClient {
7
static
System.Threading.ManualResetEvent allDone
=
new
System.Threading.ManualResetEvent(
false
);
8
[STAThread]
9
static
void
Main(
string
[] args) {
10
SocketClient.Connect();
11
12
}
13
//
连接方法
14
public
static
void
Connect() {
15
IPAddress ip
=
IPAddress.Parse(
"
10.0.0.153
"
);
16
IPEndPoint ipEP
=
new
IPEndPoint(ip,
11000
);
17
Socket client
=
new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
18
client.BeginConnect(ipEP,
new
AsyncCallback(SocketClient.ConnectCallback),client);
19
allDone.WaitOne();
20
}
21
//
异步连接
22
public
static
void
ConnectCallback(System.IAsyncResult ar) {
23
try
{
24
Socket client
=
(Socket)ar.AsyncState;
25
client.EndConnect(ar);
26
Console.WriteLine(
"
Socket connected to {0}
"
,client.RemoteEndPoint.ToString());
27
Console.WriteLine(
"
Press any key to send date to remote server
"
);
28
Console.ReadLine();
29
SocketClient.Send(client,
"
abc123
"
);
30
}
31
catch
(Exception ex) {
32
Console.WriteLine(ex.Message);
33
}
34
}
35
//
发送数据的方法
36
public
static
void
Send(Socket client,
string
data) {
37
byte
[] byteData
=
System.Text.Encoding.ASCII.GetBytes(data);
38
client.BeginSend(byteData,
0
,byteData.Length,
0
,
new
AsyncCallback(SocketClient.SendCallback),client);
39
}
40
//
异步发送方法
41
public
static
void
SendCallback(System.IAsyncResult ar) {
42
try
{
43
Socket client
=
(Socket)ar.AsyncState;
44
int
bytesSend
=
client.EndSend(ar);
45
Console.WriteLine(
"
Send {0} bytes to server.
"
,bytesSend);
46
}
47
catch
(Exception ex) {
48
Console.WriteLine(ex.Message);
49
}
50
allDone.Set();
51
}
52
53
//
异步接收与服务端异步接收一样.同理,服务端异常发送与客户端异步发送一样.
54
}
55
}
56
收藏本文:
【
大
中
小
】【
打印页面
】【推荐给朋友】【
关闭窗口
】
<< 上一篇 :
Socket异步编程-之服务器端
>> 下一篇 :
Smart Client 离线数据
相关新闻
·
ASP.NET 2.0服务器控件之复合控件事件2
·
用.Net实现基于CSS的AJAX开发(2)
·
sql2005的xml字段类型在.net中的应用1
·
实现将Web页面的内容,Export To Excel的功
·
.net的Membership,为什么就这么困难呢?
·
ASP.NET 2.0高级数据处理之数据绑定
·
sql2005的xml字段类型在.net中的应用2
·
您可能不知道的.Net2.0小技巧
最新资讯
·
ASP.NET、JSP及PHP之间的抉择
·
带你走进ASP.NET(1)
·
带你走进ASP.NET(2)
·
ASP.NET效率陷阱之——Attributes
·
ASP.NET技巧:DataGrid传统分页方式
·
asp.NET自定义服务器控件内部细节
·
ASP.NET 2.0高级数据处理之数据绑定
·
ASP.NET立即上手教程(1)
站长学院
[
ASP
]
[
PHP
]
[
JSP
]
[
CGI/perl
]
[
ASP.NET
]
[
C#
]
[
XML
]
[
编程综合
]
关 键 字:
信息分类:
新闻资讯
SEO研究
网站运营
站长学院
下载中心
推荐信息
本类热点
·
ASP.NET网站开发实用代码
·
类型“GridView”的控件“GridVi
·
用window.location.href实现刷新
·
asp.net的Request对象使用方法
·
Community Server系列之十:让CS
·
ASP.net数据库连接程序实例
·
用ASP.NET开发wap的开发心得
·
.net2.0邮件发送代码
·
三层嵌套gridview 的折叠展开
·
asp.net图片加水印