Winsock question?
-
The following is my code: void CFilerecvDlg::OnStartServer() { m_button.EnableWindow(false); CString s_name; UINT port; try { g_sListen.Create(0); g_sListen.GetSockName(s_name, port); g_sListen.Bind(port); g_sListen.Listen(); AfxBeginThread(ServerThreadProc, GetSafeHwnd()); } catch(CException* e) { e->Delete(); } } UINT ServerThreadProc(LPVOID pParam) { CSocket sConnect; g_sListen.Accept(sConnect); TRACE ("\nSocket connetced\n"); sConnect.Detach(); sConnect.Close(); return 0; } The run fails... A message is issued tellin something like "Thread 0x5425.. has exited with code -1. Memory leak detected!...". g_sListen is a global CSocket variable (in my serverdlg.cpp file). There are no warnings or errors at compile time... Can anyone tell me where is my mistake??? "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
-
The following is my code: void CFilerecvDlg::OnStartServer() { m_button.EnableWindow(false); CString s_name; UINT port; try { g_sListen.Create(0); g_sListen.GetSockName(s_name, port); g_sListen.Bind(port); g_sListen.Listen(); AfxBeginThread(ServerThreadProc, GetSafeHwnd()); } catch(CException* e) { e->Delete(); } } UINT ServerThreadProc(LPVOID pParam) { CSocket sConnect; g_sListen.Accept(sConnect); TRACE ("\nSocket connetced\n"); sConnect.Detach(); sConnect.Close(); return 0; } The run fails... A message is issued tellin something like "Thread 0x5425.. has exited with code -1. Memory leak detected!...". g_sListen is a global CSocket variable (in my serverdlg.cpp file). There are no warnings or errors at compile time... Can anyone tell me where is my mistake??? "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
Vladimir Georgiev wrote: sConnect.Detach(); sConnect.Close(); When you detach, the underlying SOCKET is detached from your CSocket object and it's m_hSocket is cleared. This itself is a leak of sorts as you dont seem to be closesocket()-ing the detached SOCKET anywhere. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
-
Vladimir Georgiev wrote: sConnect.Detach(); sConnect.Close(); When you detach, the underlying SOCKET is detached from your CSocket object and it's m_hSocket is cleared. This itself is a leak of sorts as you dont seem to be closesocket()-ing the detached SOCKET anywhere. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
UINT ServerThreadProc(LPVOID pParam) { CSocket sConnect; g_sListen.Accept(sConnect); TRACE ("\nSocket acepted\n"); sConnect.Close(); return 0; } there is no TRACE in the debugger also after executing with this piece of code... "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
-
Vladimir Georgiev wrote: sConnect.Detach(); sConnect.Close(); When you detach, the underlying SOCKET is detached from your CSocket object and it's m_hSocket is cleared. This itself is a leak of sorts as you dont seem to be closesocket()-ing the detached SOCKET anywhere. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
All the traces lead to this piece of code: // The same socket better not be blocking in more than one place. ASSERT(m_pbBlocking == NULL); _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState; ASSERT(pState->m_hSocketWindow != NULL); BOOL bBlocking = TRUE; m_pbBlocking = &bBlocking; CWinThread* pThread = AfxGetThread(); in a socketsomething.cpp "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
-
Vladimir Georgiev wrote: sConnect.Detach(); sConnect.Close(); When you detach, the underlying SOCKET is detached from your CSocket object and it's m_hSocket is cleared. This itself is a leak of sorts as you dont seem to be closesocket()-ing the detached SOCKET anywhere. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
ASSERT(pState->m_hSocketWindow != NULL); this assertion failed... If you can think of something, please post it. "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
-
UINT ServerThreadProc(LPVOID pParam) { CSocket sConnect; g_sListen.Accept(sConnect); TRACE ("\nSocket acepted\n"); sConnect.Close(); return 0; } there is no TRACE in the debugger also after executing with this piece of code... "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
Vladimir You are not doing this the right way at all. You are calling GetSockName() on an unconnected socket. That will result in weird results. I suggest that you seriously take up a book and just browse through the basic. It should take you only 10-20 mins to get adjusted to it. And one piece of advice :- The MFC socket classes are not thread-safe and you can run into all sorts of trouble using them. As far as possible use the native Winsock API. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
-
ASSERT(pState->m_hSocketWindow != NULL); this assertion failed... If you can think of something, please post it. "Needless redundancy is the hobgoblin of software engineering." - Peter Darnell
// this part is okay
g_sListen.Create(0);//not okay. you cannot call this function on
//an unconnected socket
g_sListen.GetSockName(s_name, port);//the port you got from the previous call is invalid
//your Bind() call will fail
g_sListen.Bind(port);//this function fails too cause everything previous to
//it has also failed
g_sListen.Listen();Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
-
Vladimir You are not doing this the right way at all. You are calling GetSockName() on an unconnected socket. That will result in weird results. I suggest that you seriously take up a book and just browse through the basic. It should take you only 10-20 mins to get adjusted to it. And one piece of advice :- The MFC socket classes are not thread-safe and you can run into all sorts of trouble using them. As far as possible use the native Winsock API. Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
Nish [BusterBoy] wrote: And one piece of advice :- The MFC socket classes are not thread-safe and you can run into all sorts of trouble using them. As far as possible use the native Winsock API. This is sort of true, sort of not true. The MFX Socket classes don't work very well multi threaded. But they do work. However, looks to me like you'd be far better off staying single threaded, but using CAsyncSocket. Sorry to dissapoint you all with my lack of a witty or poignant signature.
-
// this part is okay
g_sListen.Create(0);//not okay. you cannot call this function on
//an unconnected socket
g_sListen.GetSockName(s_name, port);//the port you got from the previous call is invalid
//your Bind() call will fail
g_sListen.Bind(port);//this function fails too cause everything previous to
//it has also failed
g_sListen.Listen();Nish It's seven o'clock On the dot I'm in my drop top Cruisin' the streets - Oh yeah I got a real pretty, pretty little thing that's waiting for me
Hmm, I think the sequence here should be: g_sListen.Create(0); g_sListen.Bind(....); g_sListen.GetSockName(); You can pass NULL to Bind, and let system give you the IP address of local computer as well as assign a free port number to you. Since both are assigned by the system, you want to call GetSockName to retrieve these two parameters.