MFC传文件求助

凌乱哥 2011-12-07 11:18:03
最近一直忙于做毕业设计,本来都已经差不多了的,后面经过指导老师的要求,又做了挺大的改动。。。结果,就剩下传文件这块掉链子了,不知道怎么回事冒出个问题,我跟改动前的代码做了比较,琢磨了2天了,还是没有丝毫发现。。。而且,我还单独做了个测试程序,发现是没有问题的,但是为什么放到我的毕业设计里就会崩溃。。。

主要的症状是,文件的接收方会崩溃,错误如下:
---------------------------
Feiliao.exe - 应用程序错误
---------------------------
"0x004b7203" 指令引用的 "0x00000004" 内存。该内存不能为 "read"。
要终止程序,请单击“确定”。
要调试程序,请单击“取消”。
---------------------------
确定 取消
---------------------------
点了确定后又冒出个终止框
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program:桌面\毕业设计\飞聊修改版、可语音和视频\Feiliao.exe
File: sockcore.cpp
Line: 337

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
终止(A) 重试(R) 忽略(I)
---------------------------

后面我找到sockcore.cpp的第337行:

BOOL CAsyncSocket::AsyncSelect(long lEvent)
{
ASSERT(m_hSocket != INVALID_SOCKET);

_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
ASSERT(pState->m_hSocketWindow != NULL);//////////这就是337行

return WSAAsyncSelect(m_hSocket, pState->m_hSocketWindow,
WM_SOCKET_NOTIFY, lEvent) != SOCKET_ERROR;
}


太蛋疼了。。。不知道什么情况,而且代码很多很杂,不好贴出来。那我发帖的意图就是希望找个热心人,对MFC熟悉的人,我把程序(包括我做的那个测试程序)传给你,你帮我看看是什么情况。真的就是因为这个问题卡在这里了,只要这个问题解决了,后面就可以继续做下去了。。。下月5号答辩,学校说20号要结束作品和论文,话说我论文还没下手。。。希望各路大侠紧急支援啊。。。先谢谢大家了
...全文
203 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
凌乱哥 2011-12-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 maoxing63570 的回复:]

哥,用api吧
[/Quote]
额。。。就是一般的数据发送和文件读写操作。。。
maoxing63570 2011-12-12
  • 打赏
  • 举报
回复
哥,用api吧
凌乱哥 2011-12-12
  • 打赏
  • 举报
回复
问题还没有解决。。我表示换了一种方法,没有用线程。。。结贴散分
「已注销」 2011-12-12
  • 打赏
  • 举报
回复
乱着肋,我宁愿自己做全部工作,DialogBoxParam是王道
UDX协议 2011-12-09
  • 打赏
  • 举报
回复
因为csocket是窗口消息的,所以,估计和线程操作窗口有关,

表示 观注

你的 头象,偶比较葱白!
wwzzz 2011-12-08
  • 打赏
  • 举报
回复
我以前的毕业设计就是局域网聊天的,也有传文件的功能,如果不介意可以把程序发给我看看。发我QQ邮箱447837737@qq.com
shn521 2011-12-08
  • 打赏
  • 举报
回复
是不是文件传输的太快,而处理的不及时呢。
glovel0425 2011-12-08
  • 打赏
  • 举报
回复
(转)
CSocket断言错误:ASSERT(pState->m_hSocketWindow != NULL);

起因:在套接字处于连接或者发送状态时,试图关闭套接字,于是在这个断言语句处发生中断

原因分析:This assertion failure occurs because the CSocket object was either created or accepted in the context of another thread. The socket notification window was created in a different thread, and the m_hSocketWindow for the current thread is NULL, thus the assertion failure.
实际上就是跨线程使用socket

解决方案:
As already mentioned, a CAsyncSocket object should be used only in the context of a single thread. If you need to switch the thread that is accessing a SOCKET connection with another thread, then you should use a separate CAsyncSocket object in each thread, and use the Detach and Attach functions to attach the CAsyncSocket object to the SOCKET handle in the thread that is about to use the socket. Use this sequence:
1. Use Detach() to detach the CAsyncSocket object from the SOCKET handle in the thread that is currently using the CAsyncSocket object.
2. Use Attach() to attach a different CAsyncSocket object to the SOCKET handle while in the context of the MFC UI thread in which you wish to begin accessing the SOCKET connection.
glovel0425 2011-12-08
  • 打赏
  • 举报
回复
跨线程使用CSocket得注意了.读一下下面几篇文章吧
http://support.microsoft.com/kb/140527/en-us
http://support.microsoft.com/kb/139692/en-us
http://support.microsoft.com/kb/175668/en-us
凌乱哥 2011-12-08
  • 打赏
  • 举报
回复
继续求助求助。。。
rxjyjt 2011-12-08
  • 打赏
  • 举报
回复
真复杂,本来是想学习来着,看到这么多问题,有点晕了。
《《http://www.rxjy.com/fengongsi/index.aspx?gs=12///
凌乱哥 2011-12-08
  • 打赏
  • 举报
回复
回4楼和5楼(斯巴达克斯):你给的链接我看了,似乎不存在这些问题。。。

回6楼(shn521):应该不是,来得及吧,而且以前都没错的。。

回7楼(该用户无法设置昵称):已发送。。。

楼下:还有哪位想看看的,我可以发过去,帮帮忙吧。
凌乱哥 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wutaozhao 的回复:]
接收方调试运行,崩溃的时候能根据堆栈定位到出错代码行
[/Quote]
额。。出错代码行我也知道,是在接收线程函数里。我是自己给自己发文件的时候,一步一步调试出来的:

struct SAVEFILEPARAM
{
SOCKET sock;
CTalkDlg* pDlg;
};
struct SENDFILEPARAM
{
CString pathName;
CString ip;
char fileName[NAMELENGTH];
CTalkDlg* pDlg;
};//2个结构体用于传文件

//////////////// 接收文件线程 ////////////////
UINT SaveFileSingle( LPVOID param )
{
SAVEFILEPARAM sp;
sp.pDlg=((SAVEFILEPARAM*)param)->pDlg;
sp.sock=((SAVEFILEPARAM*)param)->sock;

CSocket sock;
sock.Attach( sp.sock );//接收线程的前几行,就是调试到这里终止的。
.................//其他代码
}



//////////////// 监听有无文件传输连接。.. /////////////////
UINT ListenFileTransport( LPVOID param)
{
CTalkDlg* pCLAN=(CTalkDlg*)param;
CSocket sockServer;

if (!AfxSocketInit())
{
AfxMessageBox("ListenFileTransport AfxSocketInit( )失败。");
return 0;
}

if( 0==sockServer.Create( FILEPORT ) )
{
AfxMessageBox("监听套接字创建失败。");
return 0;
}
if( 0==sockServer.Listen( ) )
{
AfxMessageBox("监听套接字监听失败。");
sockServer.Close( );
return 0;
}

//监听套接字等待连接请求。
while( true )
{
CSocket sockClient;
if( 0==sockServer.Accept( sockClient ) )
{
AfxMessageBox("监听套接字接受服务失败。");
sockServer.Close( );
break ;
}

//用线程函数来接收文件。

/////////////////////////////////////////////////////////////////////////////
SAVEFILEPARAM param;
param.sock=sockClient.Detach( );
param.pDlg=pCLAN;

AfxBeginThread(SaveFileSingle,(LPVOID)¶m);
//////////////////////////////////////////////////////////////////////////////
}
sockServer.Close( );
return 0;
}

//还有就是在OnInitDialog里面开启监听线程的代码:
AfxBeginThread(ListenFileTransport,this);//

小麻侬 2011-12-07
  • 打赏
  • 举报
回复
接收方调试运行,崩溃的时候能根据堆栈定位到出错代码行

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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