C#.net同步异步SOCKET通讯和多线程总结(14)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
异步套接字
BeginAccept
Public IAsyncResult BeginAccept{AsyncCallback callback,object state}
AsyncCallback异步回调方法 object state自定义对象, 返回IasyncResult
Using System;
Namespace mySocket
{
Public class Stateobject
{
Public StateObject(){构造函数逻辑}
}
}à>
Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Threading;
Using System.Text;
Namespace mysocket
{
Public Class StateObject
{
Public Socket worksocket=null;
Public const int buffersize=1024;
Public byte[] buffer=new byte[buffersize];
Public StringBuilder sb=new StringBuilder();
Public StateObject()
{}
}
}
实现主机绑定和端口监听:
Private IPAddress myIP=IPAddress.Parse(“127.0.0.1”);
Private IPEndPoint MyServer;
Private Socket mySocket;
Private Socket Handler;
Private Static ManualResetEvent myreset =new ManualResetEvent(false);
Try
{
IPHostEntry myhost=new IPHostEntry();
Myhost=dns.gethostbyName(“”);
String IPString =myhost.Addresslist[0].tostring();
Myip=IPAddress.Parse(IPString);
}
Catch{MessageBox.Show(“您输入的IP地址格式不正确,重新输入!”);}
Try
{
MyServer=new IPEndPoint(myIP,Int32.Parse(“Port”));
Mysocket=new Socket(AddressFamily.Inte.Network,SocketType.Stream,Protocol.Tcp);
Mysocket.Bind(Myserver);
Mysocket.Listen(50);
Thread thread=new Thread(new ThreadStart(target));
Thread.Start();
相关新闻>>
- 发表评论
-
- 最新评论 更多>>