subject_17885.htm
来自「一些关于vc的问答」· HTM 代码 · 共 21 行
HTM
21 行
<p>
序号:17885 发表者:Frog 发表日期:2002-10-16 12:40:46
<br>主题:请教,有关CSocket和线程
<br>内容:<BR>void CSocketsView::OnSocketAnswer() <BR>{<BR> CPort Port;<BR> // Get the port number using the<BR> // dialog box.<BR> if( Port.DoModal() == IDOK ){<BR><BR> // Set up the SOCKET_INFO fields<BR> // for the thread.<BR> m_SocketInfo.bAbort = FALSE;<BR> m_SocketInfo.bListening = TRUE;<BR> m_SocketInfo.nPort = Port.m_nPort;<BR><BR> // Kick off the thread.<BR> CWinThread *lpThread =<BR> AfxBeginThread( CSocketsView::AnswerThread,<BR> (LPVOID) &m_SocketInfo );<BR><BR> // Store the thread handle for later.<BR> m_hCallAnswerThread = lpThread->m_hThread;<BR> }<BR><BR>}<BR><BR>UINT CSocketsView::AnswerThread( LPVOID lpInf )<BR>{<BR> SOCKET_INFO *lpInfo = (SOCKET_INFO *) lpInf;<BR> int nRetValue = 0;<BR> CSocket ServerSocket;<BR> BOOL bDone = FALSE;<BR> CSocket ClientSocket;<BR><BR> if( !ServerSocket.Create( lpInfo->nPort ) )//此处报错!!!<BR> ……<BR><BR><BR>}<BR><BR>我利用线程来监听客户的连接,可是执行到上面的地方出错:<BR>Unhandled exception in Sockets.exe :0xC0000005: Access Violation<BR>但是代码不写在线程中,写在按钮事件中时却不报错,为什么?<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:徐景周 回复日期:2002-10-16 13:05:39
<br>内容:追踪一下,看是否在线程中socket没有被关闭时,被create了多次所致。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Frog 回复日期:2002-10-16 14:11:29
<br>内容:不是.我重新启机后再调试这个程序也出错.<BR>另外,我不在线程中执行这段代码,却能成功不报错.<BR>Why?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:紫瑗 回复日期:2002-10-16 14:19:58
<br>内容:DEBUG进去,检查lpInf是否是合法的值,如果你的m_SocketInfo是局部变量的话,就很危险了。<BR>在线程里面搞,一定要注意同步的问题。变量的生存期要考虑清楚。<BR>UINT CSocketsView::AnswerThread( LPVOID lpInf )<BR>{<BR> SOCKET_INFO *lpInfo = (SOCKET_INFO *) lpInf;<BR> int nRetValue = 0;<BR> CSocket ServerSocket;<BR> BOOL bDone = FALSE;<BR> CSocket ClientSocket;<BR><BR> if( !ServerSocket.Create( lpInfo->nPort ) )//此处报错!!!<BR> ……<BR><BR><BR>}<BR><BR><BR> m_SocketInfo.bAbort = FALSE;<BR> m_SocketInfo.bListening = TRUE;<BR> m_SocketInfo.nPort = Port.m_nPort;<BR><BR> // Kick off the thread.<BR> CWinThread *lpThread =<BR> AfxBeginThread( CSocketsView::AnswerThread,<BR> (LPVOID) &m_SocketInfo );<BR><BR> // Store the thread handle for later.<BR> m_hCallAnswerThread = lpThread->m_hThread;<BR> }<BR><BR><BR>2002-10-16 14:20:42
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?