Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about CSocket

3 views
Skip to first unread message

Speed

unread,
Sep 2, 2002, 3:10:16 AM9/2/02
to
Hello there:

Does CScoket::Connect method "attach" the socket itself to the thread which
is calling the "Connect" method ?

The question comes from the implement function:
"CSocket::Connect method" does not return until the socket get connected
or failure. If the argument "lpzHostAddress" is not an IP-Addr but a
Hostname, the CSocket must going to resolve the domain name first. Which
will take 1 or 2 second if the hostname is unavailable. So I want to call
the "Connect" method in another Thread not in the MainWnd Thread. But it
failed.

Is there any method to solve the question ?


the source code is here:

//Header file
class CMyDlg public CDialog
{
private:
.......
CSocket m_socket;

public:
.......
static DWORD CALLBACK ConnectProc(LPVOID lpVoid);
};

//CPP file
CMyDlg::Connect()
{
m_socket
AfxBeginThread((AFX_THREADPROC)ConnectProc, (LPVOID)this);
}

DWORD CALLBACK CMyDlg::ConnectProc(LPVOID lpVoid)
{
CMyDlg *caller = (CMyDlg*)lpVoid;
caller->m_socket.Connect("desired_host", 80); // Error raise.
return 0;
}


Scott McPhillips

unread,
Sep 2, 2002, 11:24:06 AM9/2/02
to

The CSocket is "attached" to the thread that called the socket Create
function. The CSocket's methods will not work from another thread.

The best way to solve the question is to use CAsyncSocket. Its
functions return quickly, then notify you later by posting a message.
The built-in message handler calls your OnConnect when the results of
the operation are ready.

--
Scott McPhillips [VC++ MVP]

Joseph M. Newcomer

unread,
Sep 3, 2002, 2:41:43 PM9/3/02
to
Forget that CSocket exists. Use CAsyncSocket. Life is substantially easier.

The C[Async]Socket exists in a map which is thread-specific. Consequently, if you want to
pass a C[Async]Socket across a thread boundary, you must first Detach the underlying
'socket' object (the kernel socket handle) from the C[Async]Socket in the source thread,
and later Attach that socket to a new C[Async]Socket in the destination thread.. Note
that name resolution can take 70 seconds (that's the standard timeout). Hence the use of
CAsyncSocket, which decouples you from details like the resolution time. What I did in one
app was simply spawn a UI thread which handled the entire transaction, but ran with
CAsyncSocket. No boundary problems there.

You really don't want to use CSocket. You will have a lot of trouble shutting down a
CSocket connection, whereas it is trivial to shut down a CAsyncSocket connection. It is
easer to send and receive data, because you are not tying up the thread which is doing the
work at all.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Speed

unread,
Sep 3, 2002, 9:37:55 PM9/3/02
to
Thanks for Scott and Jacques.

Jacques says "a lot of trouble will be raised when shutting down it". I
really meet it. When I destroy a CSocket Object, Error raised. Tracing into
the source-code, error was right there:

BOOL CAsyncSocket::AsyncSelect(long lEvent)
{
..................
ASSERT(pState->m_hSocketWindow != NULL); //--------break at
here
..................
}
What's going on here? And how can I solve it?


Regards.

Speed.Lao


Scott McPhillips

unread,
Sep 3, 2002, 10:36:21 PM9/3/02
to

CSocket creates an invisible window that it uses to receive a message
from winsock. The assertion means that your CSocket does not have the
window. It will happen if you call a CSocket function from a thread
that did not create the socket and a CSocket whose Create called failed
for any reason. Two answers have already told you how you can solve
it. To avoid blocking your GUI for several seconds in Connect you have
to use a class you derive from CAsyncSocket.

Joseph M. Newcomer

unread,
Sep 3, 2002, 10:40:10 PM9/3/02
to
This problem is possibly related to the threading. A socket that belongs to a thread must
be closed by the thread; another thread cannot do it. Since a CAsyncSocket requires a UI
thread, it is easy to PostThreadMessage a request to close the socket, which is what I do.
You can't do this if you are using synchronous CSockets.
joe

Joseph M. Newcomer [MVP]

Speed

unread,
Sep 4, 2002, 10:28:00 AM9/4/02
to
Thanks a lot!

Best regards from Speed.Lao


0 new messages