你是不是对异步Socket 很迷惑? 看完本文的一小类 你就知道大体该做什么,怎么做了....
2012-04-08 15:35
691 查看
直接上代码,看完你就懂了。
服务器端和客户端使用同一段代码创建,是不是很简单,很easy?
Socketer Server = new Socketer(9999);
Socketer Client = new Socketer(9998, 9999);
Server.GetMsg += new GetResult(Server_GetMsg);
Client.GetMsg += new GetResult(Client_GetMsg);
然后你就send吧...都不需要Recive了...
哎,编辑的时候还不能局部选中文字,这编辑器太烂了....估计就只有速度还可以这个优势了...
public class Socketer { /// <summary> /// Server /// </summary> /// <param name="serverport">ServerPort</param> public Socketer(int serverport) { Initilize_Bind(serverport); Socket_host.Listen(1); Socket_host.BeginAccept(ar => { //这里要注意我这里上面只监听了一个连接,而下面这句也只是简答地吧原来的Socket给覆盖了,如果你要改成多客户端的话需要增加一个列表, // 并且对列表中的Socket对象分别进行相应的操作 Socket_host = Socket_host.EndAccept(ar); Recive(); }, null); } //IPAddress _HostIP; //public IPAddress HostIP { get { return _HostIP; } } /// <summary> /// Client /// </summary> /// <param name="clientport">ClientPort</param> /// <param name="serverport">ServerPort</param> public Socketer(int clientport, int serverport) { Initilize_Bind(clientport); Socket_host.BeginConnect(new IPEndPoint(IPAddress.Loopback, serverport), ar => { Socket_host.EndConnect(ar); Recive(); }, null); } private void Initilize_Bind(int hostport) { Socket_host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //_HostIP = ; Socket_host.Bind(new IPEndPoint(IPAddress.Any, hostport)); } public Socket Socket_host; //public List<Socket> Clients = new List<Socket>(); private byte[] Buffer_Recive = new byte[256]; private virtual void Recive() { if (Socket_host.Connected) { Array.Clear(Buffer_Recive, 0, Buffer_Recive.Length); Socket_host.BeginReceive(Buffer_Recive, 0, Buffer_Recive.Length, SocketFlags.None, ar => { int getsize= Socket_host.EndReceive(ar); if (GetMsg != null) { //GetMsg.Invoke(Encoding.UTF8.GetString(Buffer_Recive, 0, getsize)); //GetMsg.Method.Invoke(null, new object[]{ Encoding.UTF8.GetString(Buffer_Recive, 0, getsize)}); GetMsg(Encoding.UTF8.GetString(Buffer_Recive, 0, getsize)); //GetMsg.BeginInvoke(,null,null); } Recive(); }, null); } } public event GetResult GetMsg; public virtual void Send(string msg) { if (Socket_host.Connected) { byte[] sendbuffer = Encoding.UTF8.GetBytes(msg); Socket_host.BeginSend(sendbuffer, 0, sendbuffer.Length, SocketFlags.None, ar => { Socket_host.EndSend(ar); }, null); } } } public delegate void GetResult(string msg); //小菜另说一下,不知道为什么,写文章时,代码块 删除不掉,烦死人了
服务器端和客户端使用同一段代码创建,是不是很简单,很easy?
Socketer Server = new Socketer(9999);
Socketer Client = new Socketer(9998, 9999);
Server.GetMsg += new GetResult(Server_GetMsg);
Client.GetMsg += new GetResult(Client_GetMsg);
然后你就send吧...都不需要Recive了...
哎,编辑的时候还不能局部选中文字,这编辑器太烂了....估计就只有速度还可以这个优势了...
相关文章推荐
- 你是不是对异步Socket 很迷惑? 看完本文的一小类 你就知道大体该做什么,怎么做了....
- 你应该知道的.net平台下socket异步通讯(代码实例)
- Ubuntu 怎么知道软件被安装到什么位置?
- 14个岗位,看完就知道自己在互联网行业该怎么混
- 请教:blog上的搜索功能是不是不能用啊?我怎么什么都搜不出来?
- 数人云|项目管理多重要,看完本文就知道(程序员,项目经理必看)
- tnsnames.ora其实没有什么用,只是供plsql等工具,进行便捷访问的,那么怎么知道plsql读取的是哪个tnsnames.ora文件配置?
- colorAccent,colorPrimary,colorPrimaryDark等到底指什么?看完你就知道了
- 是不是骗局,不汇款试试怎么知道,我如果上当了再来报案好了,反正你们会负责帮我把钞票追回来的
- 全局变量和局部变量有什么区别?是怎么实现的?操作系统和编译器是怎么知道的?
- 【网虫】怎么知道网站是什么线路的?
- ”标准答案没出来,我怎么知道我想表达什么?……“这样的话,根本就不仅仅是笑话,而且还是现实
- 看完这 10 大 AI 失败案例,你就知道什么是“人工智障”了
- 怎么知道网站是用什么程序做的
- 怎么知道iphone目前是不是静音状态
- 什么是测试?看完你就知道了
- 知道塞班系统应用程序的UID,怎么知道它是哪一个应用程序,或者知道它是什么类型的应用呢
- 如果java中一个方法不知道要传递多少参数也不知道传递过来的是什么类型的数据你会怎么做?
- java网络编程中Socket什么意思?怎么工作的?