一个封装的异步Socket客户端
[转自听香水榭]
一个封装的异步Socket客户端
业务类在对此包装的socket进行访问时,由于是三个线程之间进行通讯,这个线程无法捕捉另一个线程产生的异常,所以通过信号量来通知是否有异常的产生:
一个封装的异步Socket客户端
1using System;
2using System.Collections;
3using System.Configuration;
4using System.Net;
5using System.Net.Sockets;
6using System.Text;
7
8namespace Beijing.Traffic.Mobile.Net
9{
10 /**//// <summary>
11 /// 智能手机端网络访问类
12 /// </summary>
13 public class MobileClient
14 {
15 //服务器IP地址
16 private string serverIP;
17 //服务器访问端口
18 private string serverPort;
19 private Socket socket;
20 private byte [] buffer = new byte[256];
21 private StringBuilder receiveData = null;
22 private SocketEventArgs socketEventArgs = null;
23
24 事件事件
35
36 构造函数构造函数
52
53 private void UpdateEventMessage(string msg)
54 {
55 if( socketEventArgs == null)
56 {
57 socketEventArgs = new SocketEventArgs();
58 socketEventArgs.ServerIP = this.serverIP;
59 socketEventArgs.ServerPort = this.serverPort;
60 }
61 socketEventArgs.Message = msg;
62
63 }
64
65 /**//// <summary>
66 /// 处理异常的事件
67 /// </summary>
68 /// <param name="msg"></param>
69 private void HandlerException(string msg)
70 {
71 if(OnException != null)
72 {
73 UpdateEventMessage(msg);
74 OnException(this,socketEventArgs);
75
76 }
77 else
78 {
79 throw new Exception("异常处理事件不能为空");
80 }
81 }
82
83 连接远程服务器连接远程服务器
143
144 异步发送数据异步发送数据
206
207 异步接受数据异步接受数据
271
272 接收到的数据接收到的数据
288
289 关闭socket关闭socket
309
310
311 }
312}
313
2using System.Collections;
3using System.Configuration;
4using System.Net;
5using System.Net.Sockets;
6using System.Text;
7
8namespace Beijing.Traffic.Mobile.Net
9{
10 /**//// <summary>
11 /// 智能手机端网络访问类
12 /// </summary>
13 public class MobileClient
14 {
15 //服务器IP地址
16 private string serverIP;
17 //服务器访问端口
18 private string serverPort;
19 private Socket socket;
20 private byte [] buffer = new byte[256];
21 private StringBuilder receiveData = null;
22 private SocketEventArgs socketEventArgs = null;
23
24 事件事件
35
36 构造函数构造函数
52
53 private void UpdateEventMessage(string msg)
54 {
55 if( socketEventArgs == null)
56 {
57 socketEventArgs = new SocketEventArgs();
58 socketEventArgs.ServerIP = this.serverIP;
59 socketEventArgs.ServerPort = this.serverPort;
60 }
61 socketEventArgs.Message = msg;
62
63 }
64
65 /**//// <summary>
66 /// 处理异常的事件
67 /// </summary>
68 /// <param name="msg"></param>
69 private void HandlerException(string msg)
70 {
71 if(OnException != null)
72 {
73 UpdateEventMessage(msg);
74 OnException(this,socketEventArgs);
75
76 }
77 else
78 {
79 throw new Exception("异常处理事件不能为空");
80 }
81 }
82
83 连接远程服务器连接远程服务器
143
144 异步发送数据异步发送数据
206
207 异步接受数据异步接受数据
271
272 接收到的数据接收到的数据
288
289 关闭socket关闭socket
309
310
311 }
312}
313
业务类在对此包装的socket进行访问时,由于是三个线程之间进行通讯,这个线程无法捕捉另一个线程产生的异常,所以通过信号量来通知是否有异常的产生:
1#define TEST
2
3using System;
4using System.Threading;
5using System.Text;
6using Beijing.Traffic.Mobile.Net;
7
8namespace Beijing.Traffic.Mobile.Bussiness
9{
10
11 /**//// <summary>
12 /// 业务逻辑的基类。
13 /// </summary>
14 public abstract class BussinessBase
15 {
16#if TEST
17 //手机Socket客户端
18 public MobileClient mobileClient = null;
19
20#else
21 protected MobileClient mobileClient = null;
22#endif
23
24 protected byte[] receicedBytes = null;
25 //是否产生了Socket异常
26 private bool hasException = false;
27 //异常消息类
28 private Exception socketException = null;
29
30 //三个线程通知事件
31 protected ManualResetEvent connectDone = new ManualResetEvent(false);
32 protected ManualResetEvent sendDone = new ManualResetEvent(false);
33 protected ManualResetEvent receiveDone = new ManualResetEvent(false);
34
35 public BussinessBase()
36 {
37 }
38 /**//// <summary>
39 /// 获取手机网络连接客户端类
40 /// </summary>
41 /// <returns></returns>
42 public MobileClient GetMobileClient()
43 {
44
45 if(mobileClient == null)
46 {
47 mobileClient = new MobileClient("210.73.74.200","3333");
48 mobileClient.OnConnect += new EventHandler(mobileClient_OnConnect);
49 mobileClient.OnSendFinished +=new EventHandler(mobileClient_OnSendFinished);
50 mobileClient.OnReceivedFinshed +=new EventHandler(mobileClient_OnReceivedFinshed);
51 mobileClient.OnException += new EventHandler(mobileClient_OnException);
52 }
53 return mobileClient;
54 }
55
56
57 事件事件
80
81
82 处理异常处理异常
116
117
118 业务业务
2
3using System;
4using System.Threading;
5using System.Text;
6using Beijing.Traffic.Mobile.Net;
7
8namespace Beijing.Traffic.Mobile.Bussiness
9{
10
11 /**//// <summary>
12 /// 业务逻辑的基类。
13 /// </summary>
14 public abstract class BussinessBase
15 {
16#if TEST
17 //手机Socket客户端
18 public MobileClient mobileClient = null;
19
20#else
21 protected MobileClient mobileClient = null;
22#endif
23
24 protected byte[] receicedBytes = null;
25 //是否产生了Socket异常
26 private bool hasException = false;
27 //异常消息类
28 private Exception socketException = null;
29
30 //三个线程通知事件
31 protected ManualResetEvent connectDone = new ManualResetEvent(false);
32 protected ManualResetEvent sendDone = new ManualResetEvent(false);
33 protected ManualResetEvent receiveDone = new ManualResetEvent(false);
34
35 public BussinessBase()
36 {
37 }
38 /**//// <summary>
39 /// 获取手机网络连接客户端类
40 /// </summary>
41 /// <returns></returns>
42 public MobileClient GetMobileClient()
43 {
44
45 if(mobileClient == null)
46 {
47 mobileClient = new MobileClient("210.73.74.200","3333");
48 mobileClient.OnConnect += new EventHandler(mobileClient_OnConnect);
49 mobileClient.OnSendFinished +=new EventHandler(mobileClient_OnSendFinished);
50 mobileClient.OnReceivedFinshed +=new EventHandler(mobileClient_OnReceivedFinshed);
51 mobileClient.OnException += new EventHandler(mobileClient_OnException);
52 }
53 return mobileClient;
54 }
55
56
57 事件事件
80
81
82 处理异常处理异常
116
117
118 业务业务
posted on 2007-04-18 10:13 BugHunter 阅读(4404) 评论(0) 编辑 收藏 举报