ending a blocing sockets call (am using MFC's CSocket class) 
Author Message
 ending a blocing sockets call (am using MFC's CSocket class)

In my app, ( a MFC dll) I create a thread that uses sockets. I am using MFC'
s Csocket to create a Listening socket for incoming connections. I have a
problem cleaning up the sockets due to the blocking nature of the Accept
call.

My code in the worker thread is as below (in brief)

CListenSock sockListen;    // CListenSock is derived from CSocket.

if ( 0 == sockListen.Create(g.g_sPort))
{
 nErr = WSAGetLastError();
 wsprintf(szErrString, "Socket create failure....Error number %d", nErr);
 OutputDebugString (szErrString);
 return 1;

Quote:
}

if ( 0 == sockListen.Listen())
{
 nErr = WSAGetLastError();
 wsprintf(szErrString, "Socket listen failure....Error number %d", nErr);
 OutputDebugString (szErrString);
 return 1;

Quote:
}

/* create a temp socket to accept the conncetion */

CSocket sockTemp;

sockListen.Accept(sockTemp)

 /*  read in the size of the mesg. */

TCHAR *pszBuffer= new TCHAR[sizeof(long)];

int nRecvd = sockTemp.Receive(&pPack->lMsgLen, sizeof(pPack->lMsgLen));

if (SOCKET_ERROR == nRecvd)
{
 nErr = WSAGetLastError ();
 wsprintf(szErrString, "Socket Receive failure....Error number %d", nErr);
 OutputDebugString (szErrString);

Quote:
}

char *pv = new char [pPack->lMsgLen];

/* now read in the actual mesg */

nRecvd = sockTemp.Receive(pv, pPack->lMsgLen);

if (SOCKET_ERROR == nRecvd)
{
 nErr = WSAGetLastError ();
 wsprintf(szErrString, "Socket Receive failure....Error number %d", nErr);
 OutputDebugString (szErrString);

Quote:
}

I need to end the blocing socket call in my main thread based on some
criteria. I tried to use CancelBlockingCall(), but it does not work as
required. I also tried ShutDown() but it is the same .

How do I end the blocing call, and cleanup the socket calls so that i can
reuse the port number? Now, i cannot use the same port number twice as,
obviously,  the port is in use and the Accept call blocks till the app exits
if no one connects.
Please help.

Regards,
Anil Mohan



Sat, 16 Aug 2003 01:34:54 GMT  
 ending a blocing sockets call (am using MFC's CSocket class)
Hi Anil,

I was a little confused by your code, so I will just explain how I would
achieve what you are trying.
    Firstly you neet to use CAsyncSocket, this prevents the socket blocking.
    Next we need to subclass this CAsyncSocket of the Listener so we can
overide the OnAccept() function to catch connection attempts.
    We also need to subclass CAsyncSocket of the "Accepted" socket so we can
Overide the OnClose() to clean up when it closes properly.
    Also overide the OnRecieve so we can get the input data.

In most cases it is not even necessary to create a seperate thread to do
this. (So long as you us IPAddresses and not Machine names).

There is a diagram of this at

    www.mctainsh.com/Win32/Sockets.aspx

MSDN has several good examples of creating client and server socket
connections.
--
Have a nice day.  John
~~~~~~~~~~~~~~~~~~~~~~~
I hope this is of some use. If you
do have a problem please drop me a line
or visit www.mctainsh.com
~~~~~~~~~~~~~~~~~~~~~~~


Quote:
> In my app, ( a MFC dll) I create a thread that uses sockets. I am using
MFC'
> s Csocket to create a Listening socket for incoming connections. I have a
> problem cleaning up the sockets due to the blocking nature of the Accept
> call.

> My code in the worker thread is as below (in brief)

> CListenSock sockListen;    // CListenSock is derived from CSocket.

> if ( 0 == sockListen.Create(g.g_sPort))
> {
>  nErr = WSAGetLastError();
>  wsprintf(szErrString, "Socket create failure....Error number %d", nErr);
>  OutputDebugString (szErrString);
>  return 1;
> }

> if ( 0 == sockListen.Listen())
> {
>  nErr = WSAGetLastError();
>  wsprintf(szErrString, "Socket listen failure....Error number %d", nErr);
>  OutputDebugString (szErrString);
>  return 1;
> }

> /* create a temp socket to accept the conncetion */

> CSocket sockTemp;

> sockListen.Accept(sockTemp)

>  /*  read in the size of the mesg. */

> TCHAR *pszBuffer= new TCHAR[sizeof(long)];

> int nRecvd = sockTemp.Receive(&pPack->lMsgLen, sizeof(pPack->lMsgLen));

> if (SOCKET_ERROR == nRecvd)
> {
>  nErr = WSAGetLastError ();
>  wsprintf(szErrString, "Socket Receive failure....Error number %d", nErr);
>  OutputDebugString (szErrString);
> }

> char *pv = new char [pPack->lMsgLen];

> /* now read in the actual mesg */

> nRecvd = sockTemp.Receive(pv, pPack->lMsgLen);

> if (SOCKET_ERROR == nRecvd)
> {
>  nErr = WSAGetLastError ();
>  wsprintf(szErrString, "Socket Receive failure....Error number %d", nErr);
>  OutputDebugString (szErrString);
> }

> I need to end the blocing socket call in my main thread based on some
> criteria. I tried to use CancelBlockingCall(), but it does not work as
> required. I also tried ShutDown() but it is the same .

> How do I end the blocing call, and cleanup the socket calls so that i can
> reuse the port number? Now, i cannot use the same port number twice as,
> obviously,  the port is in use and the Accept call blocks till the app
exits
> if no one connects.
> Please help.

> Regards,
> Anil Mohan



Fri, 15 Aug 2003 19:51:35 GMT  
 ending a blocing sockets call (am using MFC's CSocket class)
Hello

Yes, this is definitely a much better idea. And the code at your site is
quite helpful.

What i want to do is abruptly knock off  the "accepted" connection, that is
blocking after the accept call, and clean up sockets.

If i use the new operator to create the socket,( i see it in your code)  can
i just delete it whenever i want?

I tried to use the win32 function accept, instead of the MFC function and it
returns me a     WSAEWOULDBLOCK. So maybe i can just check this and my
condition and if it evaluates to true, exit it??

Thanks for your help. More for your website URL. Quite a number of
interesting articles.

Nice day to you too.

Anil.

Quote:
----- Original Message -----

Newsgroups: microsoft.public.vc.mfc
Sent: Monday, February 26, 2001 5:21 PM
Subject: Re: ending a blocing sockets call (am using MFC's CSocket class)

> Hi Anil,

> I was a little confused by your code, so I will just explain how I would
> achieve what you are trying.
>     Firstly you neet to use CAsyncSocket, this prevents the socket
blocking.
>     Next we need to subclass this CAsyncSocket of the Listener so we can
> overide the OnAccept() function to catch connection attempts.
>     We also need to subclass CAsyncSocket of the "Accepted" socket so we
can
> Overide the OnClose() to clean up when it closes properly.
>     Also overide the OnRecieve so we can get the input data.

> In most cases it is not even necessary to create a seperate thread to do
> this. (So long as you us IPAddresses and not Machine names).

> There is a diagram of this at

>     www.mctainsh.com/Win32/Sockets.aspx

> MSDN has several good examples of creating client and server socket
> connections.
> --
> Have a nice day.  John
> ~~~~~~~~~~~~~~~~~~~~~~~
> I hope this is of some use. If you
> do have a problem please drop me a line
> or visit www.mctainsh.com
> ~~~~~~~~~~~~~~~~~~~~~~~



Fri, 15 Aug 2003 22:15:13 GMT  
 ending a blocing sockets call (am using MFC's CSocket class)
Hello

Yes, this is definitely a much better idea. And the code at your site is
quite helpful.

What i want to do is abruptly knock off  the "accepted" connection, that is
blocking after the accept call, and clean up sockets.

If i use the new operator to create the socket,( i see it in your code)  can
i just delete it whenever i want?

I tried to use the win32 function accept, instead of the MFC function and it
returns me a     WSAEWOULDBLOCK. So maybe i can just check this and my
condition and if it evaluates to true, exit it??

Thanks for your help. More for your website URL. Quite a number of
interesting articles.

Nice day to you too.

Anil.


Quote:
> Hi Anil,

> I was a little confused by your code, so I will just explain how I would
> achieve what you are trying.
>     Firstly you neet to use CAsyncSocket, this prevents the socket
blocking.
>     Next we need to subclass this CAsyncSocket of the Listener so we can
> overide the OnAccept() function to catch connection attempts.
>     We also need to subclass CAsyncSocket of the "Accepted" socket so we
can
> Overide the OnClose() to clean up when it closes properly.
>     Also overide the OnRecieve so we can get the input data.

> In most cases it is not even necessary to create a seperate thread to do
> this. (So long as you us IPAddresses and not Machine names).

> There is a diagram of this at

>     www.mctainsh.com/Win32/Sockets.aspx

> MSDN has several good examples of creating client and server socket
> connections.
> --
> Have a nice day.  John
> ~~~~~~~~~~~~~~~~~~~~~~~
> I hope this is of some use. If you
> do have a problem please drop me a line
> or visit www.mctainsh.com
> ~~~~~~~~~~~~~~~~~~~~~~~



> > In my app, ( a MFC dll) I create a thread that uses sockets. I am using
> MFC'
> > s Csocket to create a Listening socket for incoming connections. I have
a
> > problem cleaning up the sockets due to the blocking nature of the Accept
> > call.

> > My code in the worker thread is as below (in brief)

> > CListenSock sockListen;    // CListenSock is derived from CSocket.

> > if ( 0 == sockListen.Create(g.g_sPort))
> > {
> >  nErr = WSAGetLastError();
> >  wsprintf(szErrString, "Socket create failure....Error number %d",
nErr);
> >  OutputDebugString (szErrString);
> >  return 1;
> > }

> > if ( 0 == sockListen.Listen())
> > {
> >  nErr = WSAGetLastError();
> >  wsprintf(szErrString, "Socket listen failure....Error number %d",
nErr);
> >  OutputDebugString (szErrString);
> >  return 1;
> > }

> > /* create a temp socket to accept the conncetion */

> > CSocket sockTemp;

> > sockListen.Accept(sockTemp)

> >  /*  read in the size of the mesg. */

> > TCHAR *pszBuffer= new TCHAR[sizeof(long)];

> > int nRecvd = sockTemp.Receive(&pPack->lMsgLen, sizeof(pPack->lMsgLen));

> > if (SOCKET_ERROR == nRecvd)
> > {
> >  nErr = WSAGetLastError ();
> >  wsprintf(szErrString, "Socket Receive failure....Error number %d",
nErr);
> >  OutputDebugString (szErrString);
> > }

> > char *pv = new char [pPack->lMsgLen];

> > /* now read in the actual mesg */

> > nRecvd = sockTemp.Receive(pv, pPack->lMsgLen);

> > if (SOCKET_ERROR == nRecvd)
> > {
> >  nErr = WSAGetLastError ();
> >  wsprintf(szErrString, "Socket Receive failure....Error number %d",
nErr);
> >  OutputDebugString (szErrString);
> > }

> > I need to end the blocing socket call in my main thread based on some
> > criteria. I tried to use CancelBlockingCall(), but it does not work as
> > required. I also tried ShutDown() but it is the same .

> > How do I end the blocing call, and cleanup the socket calls so that i
can
> > reuse the port number? Now, i cannot use the same port number twice as,
> > obviously,  the port is in use and the Accept call blocks till the app
> exits
> > if no one connects.
> > Please help.

> > Regards,
> > Anil Mohan



Fri, 15 Aug 2003 22:16:17 GMT  
 ending a blocing sockets call (am using MFC's CSocket class)
Hi Anil,

If you use the new operator to create an CAsyncSocket you can abruptly
delete and that should usually clean it up correctly.

Note: WSAEWOULDBLOCK is not always an fatal error condition. It often only
means the operation cannot complete immediately but probably will in its own
good time. This is often the case when a Connect is called and
WSAEWOULDBLOCK is returned. It means OnConnect will fire once connected.

I created the Win32 based socket class for three reasons;
    1) Its a good way to understand a little of the inner workings of
sockets.
    2) It works without MFC.
    3) The MFC sockets have a small problem when connecting and
disconnecting at high rates. (>50 times/second). It is very rare that this
would be a problem and is probably fixed by now.

My example does have a problem in it. The fix is in the comments and relates
to the "127.0.0.1" look up function.

--
Have a nice day.  John
~~~~~~~~~~~~~~~~~~~~~~~
I hope this is of some use. If you
do have a problem please drop me a line
or visit www.mctainsh.com
~~~~~~~~~~~~~~~~~~~~~~~


Quote:
> Hello

> Yes, this is definitely a much better idea. And the code at your site is
> quite helpful.

> What i want to do is abruptly knock off  the "accepted" connection, that
is
> blocking after the accept call, and clean up sockets.

> If i use the new operator to create the socket,( i see it in your code)
can
> i just delete it whenever i want?

> I tried to use the win32 function accept, instead of the MFC function and
it
> returns me a     WSAEWOULDBLOCK. So maybe i can just check this and my
> condition and if it evaluates to true, exit it??

> Thanks for your help. More for your website URL. Quite a number of
> interesting articles.

> Nice day to you too.

> Anil.



> > Hi Anil,

> > I was a little confused by your code, so I will just explain how I would
> > achieve what you are trying.
> >     Firstly you neet to use CAsyncSocket, this prevents the socket
> blocking.
> >     Next we need to subclass this CAsyncSocket of the Listener so we can
> > overide the OnAccept() function to catch connection attempts.
> >     We also need to subclass CAsyncSocket of the "Accepted" socket so we
> can
> > Overide the OnClose() to clean up when it closes properly.
> >     Also overide the OnRecieve so we can get the input data.

> > In most cases it is not even necessary to create a seperate thread to do
> > this. (So long as you us IPAddresses and not Machine names).

> > There is a diagram of this at

> >     www.mctainsh.com/Win32/Sockets.aspx

> > MSDN has several good examples of creating client and server socket
> > connections.
> > --
> > Have a nice day.  John
> > ~~~~~~~~~~~~~~~~~~~~~~~
> > I hope this is of some use. If you
> > do have a problem please drop me a line
> > or visit www.mctainsh.com
> > ~~~~~~~~~~~~~~~~~~~~~~~



> > > In my app, ( a MFC dll) I create a thread that uses sockets. I am
using
> > MFC'
> > > s Csocket to create a Listening socket for incoming connections. I
have
> a
> > > problem cleaning up the sockets due to the blocking nature of the
Accept
> > > call.

> > > My code in the worker thread is as below (in brief)

> > > CListenSock sockListen;    // CListenSock is derived from CSocket.

> > > if ( 0 == sockListen.Create(g.g_sPort))
> > > {
> > >  nErr = WSAGetLastError();
> > >  wsprintf(szErrString, "Socket create failure....Error number %d",
> nErr);
> > >  OutputDebugString (szErrString);
> > >  return 1;
> > > }

> > > if ( 0 == sockListen.Listen())
> > > {
> > >  nErr = WSAGetLastError();
> > >  wsprintf(szErrString, "Socket listen failure....Error number %d",
> nErr);
> > >  OutputDebugString (szErrString);
> > >  return 1;
> > > }

> > > /* create a temp socket to accept the conncetion */

> > > CSocket sockTemp;

> > > sockListen.Accept(sockTemp)

> > >  /*  read in the size of the mesg. */

> > > TCHAR *pszBuffer= new TCHAR[sizeof(long)];

> > > int nRecvd = sockTemp.Receive(&pPack->lMsgLen,

sizeof(pPack->lMsgLen));

- Show quoted text -

Quote:

> > > if (SOCKET_ERROR == nRecvd)
> > > {
> > >  nErr = WSAGetLastError ();
> > >  wsprintf(szErrString, "Socket Receive failure....Error number %d",
> nErr);
> > >  OutputDebugString (szErrString);
> > > }

> > > char *pv = new char [pPack->lMsgLen];

> > > /* now read in the actual mesg */

> > > nRecvd = sockTemp.Receive(pv, pPack->lMsgLen);

> > > if (SOCKET_ERROR == nRecvd)
> > > {
> > >  nErr = WSAGetLastError ();
> > >  wsprintf(szErrString, "Socket Receive failure....Error number %d",
> nErr);
> > >  OutputDebugString (szErrString);
> > > }

> > > I need to end the blocing socket call in my main thread based on some
> > > criteria. I tried to use CancelBlockingCall(), but it does not work as
> > > required. I also tried ShutDown() but it is the same .

> > > How do I end the blocing call, and cleanup the socket calls so that i
> can
> > > reuse the port number? Now, i cannot use the same port number twice
as,
> > > obviously,  the port is in use and the Accept call blocks till the app
> > exits
> > > if no one connects.
> > > Please help.

> > > Regards,
> > > Anil Mohan



Sat, 16 Aug 2003 04:40:52 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Calling Oracle stored procedures using MFC's classes

2. CSockets: Using more than one socket class in an App

3. COM object that uses MFC and MFC Socket class

4. COM object that uses MFC and MFC Socket class

5. Using MFC CSockets created by main program in a dll....I'm at a loss

6. Socket Programming in mfc--receiving end

7. Can't create socket with CSocket

8. CSocket won't create a valid socket

9. MFC ended up calling ATL in Visual .NET

10. Any help on using 'SOCKET'

11. CSocket ->OnReceive don't always get called

12. CSocket class 's member variable m_nTimeOut

 

 
Powered by phpBB® Forum Software