private IPEndPoint localEndPoint;
private Socket mySocket;
private bool B_receive_Frm= fase;
public Thread LstThReceive = null; //启动接收
protected void StartTcpRec()
{ IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
localEndPoint = new IPEndPoint(ipAddress, 5000);
mySocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(localEndPoint);
try
{
LstThReceive = new Thread(new ThreadStart(Listerner));
LstThReceive.IsBackground = true;
B_receive_Frm = true;
LstThReceive.Start();
}
catch (Exception e)
{
throw new Exception(" Start SOCKET:" + e.Message);
} } /// <summary>
/// 监听数据
/// </summary>
protected void Listerner()
{
try
{
mySocket.Listen(100);
while (B_receive_Frm == true)
{
// Set the event to nonsignaled state.
allDone.Reset(); // Start an asynchronous socket to listen for connections.
// Console.WriteLine("Waiting for a connection...");
mySocket.BeginAccept(
new AsyncCallback(AcceptCallback),
mySocket); // Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (System.ObjectDisposedException e)
{
throw new Exception("SOCKET OBJECT : " + e.Message);
}
catch (Exception ex)
{
throw new Exception(" SOCKET Listerner: " + ex.Message);
}
}
/// <summary>
/// 异步接受数据
/// </summary>
/// <param name="ar"></param>
protected void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set(); //下面就是异步处理的一些代码就不贴了. }首先请大家看一下代码对不,想用线程实现socket异步通信功能!在主界面调用 StartTcpRec 方法后,主界面还有其他线程在处理另外的工作.
单机测试,就是说长时间不给socket发送信息(比如:2-3小时或者更长时间)
发现出现如下问题:
Message:[ObjectDisposedException]
stackTrace:
at system.threading.waitHandle.checkResultInternal(boolean r)
at system.threading.eventwaithandle.set()
at system.windows.forms.control.task.done()
at system.windows.forms.control.task.invoke()
at system.windows.forms.control._invokeall()
at system.windows.forms.control.wnproc(WM wm,int32 wparam,int32 Iparam)
at system.windows.forms.containercontrol.wnproc(WM wm,int32 wparam,int32 iparam)
at system.windows.forms.control._internalwnproc(wm wm,int32 wparam,int32 iparam)
at miscrosoft.agl.forms.evl.entermainloop(intptr hwnmain)
at system.windows.forms.all.........在百度后,有网友说可能是 " allDone.WaitOne(); " 长时间等待会出现这个问题.请问大虾们有没有遇到这种情况,或者我的代码写的有问题?
有朋友说可以采用 alldone.waitone(new TimeSpan(0,30,0),true) 这样来处理,不知道这样是否可行?或者还有更好的处理方法?谢谢大家了!
private Socket mySocket;
private bool B_receive_Frm= fase;
public Thread LstThReceive = null; //启动接收
protected void StartTcpRec()
{ IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
localEndPoint = new IPEndPoint(ipAddress, 5000);
mySocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(localEndPoint);
try
{
LstThReceive = new Thread(new ThreadStart(Listerner));
LstThReceive.IsBackground = true;
B_receive_Frm = true;
LstThReceive.Start();
}
catch (Exception e)
{
throw new Exception(" Start SOCKET:" + e.Message);
} } /// <summary>
/// 监听数据
/// </summary>
protected void Listerner()
{
try
{
mySocket.Listen(100);
while (B_receive_Frm == true)
{
// Set the event to nonsignaled state.
allDone.Reset(); // Start an asynchronous socket to listen for connections.
// Console.WriteLine("Waiting for a connection...");
mySocket.BeginAccept(
new AsyncCallback(AcceptCallback),
mySocket); // Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (System.ObjectDisposedException e)
{
throw new Exception("SOCKET OBJECT : " + e.Message);
}
catch (Exception ex)
{
throw new Exception(" SOCKET Listerner: " + ex.Message);
}
}
/// <summary>
/// 异步接受数据
/// </summary>
/// <param name="ar"></param>
protected void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set(); //下面就是异步处理的一些代码就不贴了. }首先请大家看一下代码对不,想用线程实现socket异步通信功能!在主界面调用 StartTcpRec 方法后,主界面还有其他线程在处理另外的工作.
单机测试,就是说长时间不给socket发送信息(比如:2-3小时或者更长时间)
发现出现如下问题:
Message:[ObjectDisposedException]
stackTrace:
at system.threading.waitHandle.checkResultInternal(boolean r)
at system.threading.eventwaithandle.set()
at system.windows.forms.control.task.done()
at system.windows.forms.control.task.invoke()
at system.windows.forms.control._invokeall()
at system.windows.forms.control.wnproc(WM wm,int32 wparam,int32 Iparam)
at system.windows.forms.containercontrol.wnproc(WM wm,int32 wparam,int32 iparam)
at system.windows.forms.control._internalwnproc(wm wm,int32 wparam,int32 iparam)
at miscrosoft.agl.forms.evl.entermainloop(intptr hwnmain)
at system.windows.forms.all.........在百度后,有网友说可能是 " allDone.WaitOne(); " 长时间等待会出现这个问题.请问大虾们有没有遇到这种情况,或者我的代码写的有问题?
有朋友说可以采用 alldone.waitone(new TimeSpan(0,30,0),true) 这样来处理,不知道这样是否可行?或者还有更好的处理方法?谢谢大家了!
你好!你的意思是另外启动一个线程来监控,在刚刚出问题那里catch一下做个标志,如果新起的线程发现这个标志就把原来的接收线程停止,再重起一下?这是个变通的方法.不知道是否有更好的方法呢?
你好!你的意思是另外启动一个线程来监控,在刚刚出问题那里catch一下做个标志,如果新起的线程发现这个标志就把原来的接收线程停止,再重起一下?这是个变通的方法.不知道是否有更好的方法呢?
[/Quote]不用再开一个线程了,浪费资源
alldone.waitone(new TimeSpan(0,30,0),true)的作法也是可以的.
提示我参数不对 ,后来第2个参数换成false就行了。
int bytesRead = mySocket.EndReceive(ar);
if (bytesRead > 0)
{
.......
}
else{
mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
//throw(new Exception("读入的数据小于1bit"));
}
用心跳包可以解决。
/// 异步接受数据
/// </summary>
/// <param name="ar"></param>
protected void AcceptCallback(IAsyncResult ar)
{
mySocket.BeginAccept(
new AsyncCallback(AcceptCallback),
mySocket); //下面就是异步处理的一些代码就不贴了. }