Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Winsock question?

Winsock question?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++debuggingperformance
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vladimir Georgiev
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • V Vladimir Georgiev

      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

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      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

      V 3 Replies Last reply
      0
      • N Nish Nishant

        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

        V Offline
        V Offline
        Vladimir Georgiev
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • N Nish Nishant

          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

          V Offline
          V Offline
          Vladimir Georgiev
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • N Nish Nishant

            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

            V Offline
            V Offline
            Vladimir Georgiev
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • V Vladimir Georgiev

              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

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              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

              J 1 Reply Last reply
              0
              • V Vladimir Georgiev

                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

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #7

                // 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

                L 1 Reply Last reply
                0
                • N Nish Nishant

                  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

                  J Offline
                  J Offline
                  Jon Hulatt
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • N Nish Nishant

                    // 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

                    L Offline
                    L Offline
                    lucy 0
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups