当socket中断,开启一个定时器去重连,用的定时器是timeSetEvent。在回调函数中先closesocket然后重新连接,问题是根据connect的返回值看是连接上了,但实际是没连接上。回调函数部分如下:
其中pthis的声明为:static CRemotTran *pthis;
- C/C++ code
void CALLBACK CRemotTran::ReConTimerPro(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwtime){ //AfxMessageBox(_T("OnTimer")); try { closesocket(pthis->m_sScoket); } catch (CException* e) { AfxMessageBox(_T("关闭socket失败")); } pthis->m_sScoket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); //socket初始化 if(pthis->m_sScoket==INVALID_SOCKET) return; if(connect(pthis->m_sScoket,(LPSOCKADDR)&pthis->m_adServer,sizeof(LPSOCKADDR))) { timeKillEvent(pthis->m_uiReConTimer); //关闭定时器 pthis->m_bReConTimerFlag=false; pthis->m_bSockConFlag=true; }}
------解决方案--------------------
恩,了解了