关于Socket使用中出现到的无法捕捉的异常 困扰了很多天了......

这个还能被注册? 2009-04-23 09:47:14
Function: BLL.netWork.SendMsg(string, bool), Thread: 0x524 <无名称> "<msg><index>2644</index><marker>C</marker></msg>"{2009-4-23 20:41:04}
Function: BLL.netWork.AcceptMsg(), Thread: 0x354 <无名称> "<msg><index>30</index><marker>C</marker></msg>"{2009-4-23 20:41:04}
Function: BLL.netWork.SendMsg(string, bool), Thread: 0x524 <无名称> "<msg><index>2645</index><marker>C</marker></msg>"{2009-4-23 20:44:04}//正在与服务器正常通讯,后面是通讯时间
“KeFuServer.vshost.exe”(托管): 已加载“C:\WINDOWS\assembly\GAC_MSIL\System.resources\2.0.0.0_zh-CHS_b77a5c561934e089\System.resources.dll”//开始加载文件了
在 System.IO.IOException 中第一次偶然出现的“System.dll”类型的异常
线程 '<无名称>' (0x354) 已退出,返回值为 0 (0x0)。//该死的异常!!!
在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常//退出了
在 System.Threading.ThreadAbortException 中第一次偶然出现的“BLL.dll”类型的异常
线程 '<无名称>' (0x524) 已退出,返回值为 0 (0x0)。
线程 0x8f8 已退出,返回值为 0 (0x0)。
线程 0x14ec 已退出,返回值为 0 (0x0)。
程序“[5332] KeFuServer.vshost.exe: 托管”已退出,返回值为 0 (0x0)。

以上是部分异常信息,目前这个项目是用的socket的tcp/ip链接,开始的一段时间没有问题,但是在运行了1到2个小时以后在程序端出现第一行的信息(在调试窗口),然后就报异常,而且这个异常并不能被捕获。按照路径打开文件夹 结果发现并不存在该目录。下面的两个异常是在终止线程的时候发生的,大家不必理会。现在最主要的问题是 这个文件到底是怎么回事?怎么一加载就报异常并退出呢?而且这个路径并不存在,郁闷的很...请大家多多帮忙 谢谢
...全文
747 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
sujingliunian 2011-08-04
  • 打赏
  • 举报
回复
如何解决这个问题啊?楼主~跪求ing
春天的气息 2009-04-26
  • 打赏
  • 举报
回复

看看吧,sockets专题。

http://bbs.bbs180.com/topictag-29.aspx
from911cs 2009-04-26
  • 打赏
  • 举报
回复
不懂,顶顶
  • 打赏
  • 举报
回复
算了 结贴吧...
gxj760998 2009-04-26
  • 打赏
  • 举报
回复
socket = tcp.AcceptSocket();
线程就是在这里停滞了,因为在等待下一个连接请求。问题是:如果长时间没有请求的话,系统就会强行停止该线程
建议LZ弄清楚为什么系统会这样处理,这样就深入进去,学到不少东西。
wartim 2009-04-25
  • 打赏
  • 举报
回复
而复杂的
wartim 2009-04-25
  • 打赏
  • 举报
回复
只是习惯问题,我习惯简单应用用tcplister/clienet和它自己封装好的accepttcpclient之类的
而负责的直接用 socket,可以定制很多参数,写起来复杂点,我没这么混用过

这个lock没意义吧,难道你有多个线程会调用StartListening?

StartListening本身是不是也是个线程?
好像你把socket 定义成公共了吧,应该在thread2操作这个socket的地方lock(静态对象)
或者应该每个thread2对应一个收到的socket去处理吧,不然只有一个socket在处理的话,也不用开第2个线程了么
NeptuneGrass 2009-04-25
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 wartim 的回复:]
你AcceptSocket部分的代码在哪里?怎么没有
而且如果要用TCPLister/TCPClient用 TCPClient Client = Listener.AcceptTcpClient()比较好不要用它的AcceptSocket()
要用Socket用Socket的Socket S;S.Accept()
[/Quote]

请问两者有什么区别吗?能否请详细说明下?
这是我的服务器端方法
/// <summary>
/// 开始对指定的端口进行监听。
/// </summary>
protected void StartListening()
{
tcp = new TcpListener(IPAddress.Parse(ConfigurationManager.AppSettings["webip"]), this._prot);
tcp.ExclusiveAddressUse = false;
tcp.Start(20);

lock (objTemp)
{
while (isrun)
{
try
{
if (tcp.Pending())
{
socket = tcp.AcceptSocket();
thread2 = new Thread(new ThreadStart(ServiceClient));
thread2.Priority = ThreadPriority.Lowest;
thread2.IsBackground = false;
thread2.Start();
}
}
catch (System.InvalidOperationException ioe)
{
string err = "\r\nTime:" + DateTime.Now.ToString() + "\r\nSource:" + ioe.Source + " \r\n Message: " + ioe.Message + " \r\n StackTrace: " + ioe.StackTrace + "\r\nStartListening InvalidOperationException\r\n";
Server.WriteToEventLog(err);//写入错误日志
}
catch (System.Net.Sockets.SocketException se)
{
string err = "\r\nTime:" + DateTime.Now.ToString() + "\r\nSource:" + se.Source + " \r\n Message: " + se.Message + " \r\n StackTrace: " + se.StackTrace + "\r\nStartListening SocketException\r\n";
Server.WriteToEventLog(err);//写入错误日志
}
catch (Exception e)
{
string err = "\r\nTime:" + DateTime.Now.ToString() + "\r\nSource:" + e.Source + " \r\n Message: " + e.Message + " \r\n StackTrace: " + e.StackTrace + "\r\nStartListening Exception\r\n";
Server.WriteToEventLog(err);//写入错误日志
}
}
}
}
冰凝瞬间1986 2009-04-25
  • 打赏
  • 举报
回复
友情客串
zzxap 2009-04-25
  • 打赏
  • 举报
回复
线程管理要注意用lock方法,排斥资源竞争
wartim 2009-04-25
  • 打赏
  • 举报
回复
你AcceptSocket部分的代码在哪里?怎么没有
而且如果要用TCPLister/TCPClient用 TCPClient Client = Listener.AcceptTcpClient()比较好不要用它的AcceptSocket()
要用Socket用Socket的Socket S;S.Accept()
sxmonsy 2009-04-25
  • 打赏
  • 举报
回复
自己把线程停掉,然后返回个信息说我没等到请求。
wartim 2009-04-25
  • 打赏
  • 举报
回复
不会吧,是其它原因引起来的吧,我有一个winform系统其中有个windows服务线程死循环方式在监听接受和处理命令,有时一多个月没发给它命令过了还在那AcceptSocket
  • 打赏
  • 举报
回复
我现在都不知道是哪里出的问题,异常信息是在输出窗口显示的...
  • 打赏
  • 举报
回复
/// <summary>
/// 侦听端口
/// </summary>
protected void AcceptMsg()
{
bool blistener = true;
while (blistener)
{
if (stream == null)
{
blistener = false;
continue;
}

Byte[] buffer = new Byte[40960];
try
{
int ss = stream.Read(buffer, 0, buffer.Length);
string text = SerlizerHelper.Deserialize(buffer).ToString();
buffer = null;
HandleXml xml = new HandleXml();
try
{
xml.Load(text);
}
catch
{
continue;
}
if (EventGetData != null)
EventGetData(text, xml);//获得数据后进行处理
}
catch (SocketException ex)
{
throw ex;
}
catch (Exception e)
{
throw e; }
}
}
}
  • 打赏
  • 举报
回复
 发送连接请求:listener = new TcpClient();
try
{
if (!listener.Connected)
{
listener.Connect(new IPEndPoint(IPAddress.Parse(ip), prot));
}

stream = listener.GetStream();

thread1 = new Thread(new ThreadStart(AcceptMsg));
thread1.IsBackground = false;
thread1.Priority = ThreadPriority.Lowest;
thread1.Start();


return 1;
}
catch (SocketException ex)
{
throw ex;
}
catch (Exception e)
{
throw e;
}
}
deng520159 2009-04-24
  • 打赏
  • 举报
回复
问题看似简单,但是一看却有点迷糊,顶一下,
hm020 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 agentianle 的回复:]
复杂问题上代码才是王道

把出问题的那个线程执行的方法贴出来看看
[/Quote]

经典.
gxj760998 2009-04-24
  • 打赏
  • 举报
回复
怀疑是错误操作了SOCKET引起的,贴点代码出来吧。
我也在用SOCKET通讯,没有出现过LZ这样的问题。
加载更多回复(21)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧