Hi Guys, This same problem has driven me nuts but finally i fixed it and it is working. This is bug with MFC socket library that when inside a thread [other than main application thread], If we try to do something like CSocket socket; socket.Create();
It will throws an unhandled exception. I found an article on it See What Microsoft says about this that said something from Microsoft but that did not help me either. So here is a workaround i have found and i hope it can help some frustrated fellow like me. Inside thread, do this CSocket mySock; SOCKET sockethandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); mySock.m_hSocket= sockethandle;
After that DO NOT call mySock.Create as it has been created already through assignment of socket handle. I am not sure if we can use mySock.Attach(sockethandle) as i did not try it yet. After that you can call Connect etc directly. When you are done using the socket, DO NOT call
- rather call closesocket(mySock.m_hSocket);
And that will free the socket object. If Attach works in above case then i guess we need to do Detach here when to free the socket. Good Luck Nauman Khan