MFC CSocket Connect() hangs.
Posted on 2006-05-03
I'm having a problem with CSocket that I can't resolve, it seems easy enough and probably is but I'm completely stuck.
I have a server and a client application, the client contacts the server when it needs data, the data is sent using the MFC classes CFileSocket, CArchive and CSocket and the size of the data is between 10 KB and 2000 KB depending on what is requested.
The code snippet below shows where the problem is:
1 if (!socket.Create())
2 throw CMyException(_T("socket.Create"), socket.GetLastError());
4 if (!socket.Connect(server, port)) // HANGS!
5 throw CMyException(_T("socket.Connect"), socket.GetLastError());
The program doesn't always hang on row 4, usually it hangs after 2-3 requests, if I restart the server it works fine again.
Interestingly, if I put a breaktpoint on row 4 and wait a while or step into socket.Connect() everything works fine so it seems like there is some timing problem.
Another interesting thing I have noticed is that if I send small pieces of data the program doesn't hang, perhaps it will hang if I repeat the process many,many times but there is no indication of it as I far as I can tell.