CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Posts
    11

    CSocket and Mutlithread problem.

    I have a program that have a control thread and some connection threads. Then user stop connections though control thread, I hope control thread can stop connection threads and close CSocket, I write these line:

    typedef struct _CONNECTION_THREAD
    {
    CSocket * connectionSocket;
    CMyObject * myObject;
    } CONNECTION_THREAD;

    UINT controlThread(LPARAM lParam)
    {
    CONNECTION_THREAD connectThreadStruct;
    CWinThread connectThread;
    connectThread = ::AfxBeginThread(conntectFunction, &connectThreadStruct);

    MSG msg;
    while (TRUE)
    {
    ::GetMessage(&msg, NULL, 0, 0);
    switch (msg.message)
    {
    case WM_TIMER:
    .
    .
    case WM_DISCONNECT:
    ::TerminateThread(connectThread.m_hThread, 0);
    if (connectThreadStruct.connectSocket != NULL)
    {
    connectThreadStruct.connectSocket->Close();
    delete connectThreadStruct.connectSocket;
    connectThreadStruct.connectSocket = NULL
    }
    break;
    .
    .
    .
    }
    }

    UINT conntectFunction(LPARAM lParam)
    {
    CONNECT_THREAD* connectThreadStruct = (CONNECT_THREAD *)lParam;
    connectThreadStruct->connectSocket = new CSocket();
    connectThreadStruct->connectSocket->Create();
    // begin to connect to server and logon and then receive some data... ...
    // it can't get other message here
    }

    but it still have problem with CSocket's close function, I had try put TerminateThread after CSocket's close but still have problem. If I don't close it but only delete it, sometime it will failed then I create another CSocket, error code is 10055, that means "No buffer space is available. The socket cannot be created." I don't know if it because I haven't close some Socket before I delete it.
    Who can tell me why? I know better not use TerminateThread, but I must control timeout and user stop here, so I must use it.
    Thanks.


  2. #2
    Join Date
    Jul 2000
    Location
    NJ USA
    Posts
    5

    Re: CSocket and Mutlithread problem.


    You can try two aproaches
    1. Try to play with setsockopt(...). You need to set LINGER option to avoid 'Graceful Shutdown'.
    2. Switch to non-blocking sockets calls and use timeouts. In this case if there is no communication in timeout period function will return and you will be able to clean up inside working thread.

    In both cases you need to find a way how to notified client that server about to shut down.


  3. #3
    Join Date
    Jul 2000
    Posts
    7

    Re: CSocket and Mutlithread problem.

    I was looking for server code that would create a CAsyncSocket to listen and spawn CSocket worker threads and I found one on Microsoft's site called MultiSoc.exe and it is exactly what you are looking for. If a client closes - the server is aware of it and if the server closes - it will shutdown all of the clients.

    http://support.microsoft.com/support.../Q175/6/68.ASP

    I ran it and used SPY++ to confirm the worker sockets are on threads and it works.

    Hope it helps.
    Paul



  4. #4
    Guest

    Re: CSocket and Mutlithread problem.

    This doesn't answer your question, but it might save you some trouble later...

    You are asking for trouble starting your thread with a pointer (&connectThreadStruct) to an object that is on the stack frame. It is much better to place this object on the heap. I'm sure some of the guru's would be happy to explain why. You'll probably see why yourself if you think about it a little bit.

    Happy Coding



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured