CAsyncSocket or CSocket example in a console application with MFC support 
Author Message
 CAsyncSocket or CSocket example in a console application with MFC support

    Hi!

    I created a class CMySocket that inherits CAsyncSocket and overrides
OnAccept and OnReceive methods. In a console application that supports MFC I
created a object from this class and called the methods

    myobj->Create(64000);    //port = 64000
    myobj->Listen(5);

    and after this I added and infinite loop, but nothing happens when a
connection is initialized with a client.

    How can I make in a console application with a MFC support, my
application to be interrupted when the listening socket accepts a connection
or receives data (using CAsyncSocket or CSocket)?

    Best regards,
        Martin



Mon, 25 Apr 2005 03:38:23 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support

Quote:

>     Hi!

>     I created a class CMySocket that inherits CAsyncSocket and overrides
> OnAccept and OnReceive methods. In a console application that supports MFC I
> created a object from this class and called the methods

>     myobj->Create(64000);    //port = 64000
>     myobj->Listen(5);

>     and after this I added and infinite loop, but nothing happens when a
> connection is initialized with a client.

>     How can I make in a console application with a MFC support, my
> application to be interrupted when the listening socket accepts a connection
> or receives data (using CAsyncSocket or CSocket)?

>     Best regards,
>         Martin

The CAsyncSocket class uses the MFC message pump to receive the
notifications.  This pump does not usually run in a console app.  (It
also does not run in a windowed app while your code is in an infinite
loop.)  CSocket has the same requirements.  The idea of an asynchronous
socket is to notify an event-driven program when the socket needs
attention.  But a console app is not event-driven.  Probably the easiest
solution is to use a synchronous socket with winsock calls instead of
MFC calls.

--
Scott McPhillips [VC++ MVP]



Mon, 25 Apr 2005 13:49:11 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support
Why 5? Why not SO_MAXCONN? Also, you should avoid hard-wiring port numbers whenever
possible.

What I don't understand is why you would go into an "infinite loop"? This guarantees that
you will get no events.

In a console app, you can't use this unless you create a UI thread to handle the socket
communication, because you need a message pump to make CAsyncSocket work. It is not clear
what your main program should be doing at this point, but an infinite loop is a really
losing idea since all it will do is suck down CPU cycles without doing anything useful.
You might consider waiting for the UI thread to terminate, but creating CAsyncSocket in
console apps is very unnatural. What do you have against GUI apps?
                                joe

Quote:

>    Hi!

>    I created a class CMySocket that inherits CAsyncSocket and overrides
>OnAccept and OnReceive methods. In a console application that supports MFC I
>created a object from this class and called the methods

>    myobj->Create(64000);    //port = 64000
>    myobj->Listen(5);

>    and after this I added and infinite loop, but nothing happens when a
>connection is initialized with a client.

>    How can I make in a console application with a MFC support, my
>application to be interrupted when the listening socket accepts a connection
>or receives data (using CAsyncSocket or CSocket)?

>    Best regards,
>        Martin

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Mon, 25 Apr 2005 18:32:17 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support

    The code that I wrote was a simple example (not real code). But I needed
a TCP server in
a console application, that will go in an infinite loop and also do
something in that loop besides waiting for a connection.
    It seems like every class in MFC is an event driven class. I thought of
using CWinThread, but also I would have not achieved anything.

    In Java it is so simple if I use threads in a console application.

    I managed to do this with a nonblocking sockets using Berkley's socket
routines.

    Thank you for your time,
        Martin



Quote:
> Why 5? Why not SO_MAXCONN? Also, you should avoid hard-wiring port numbers
whenever
> possible.

> What I don't understand is why you would go into an "infinite loop"? This
guarantees that
> you will get no events.

> In a console app, you can't use this unless you create a UI thread to
handle the socket
> communication, because you need a message pump to make CAsyncSocket work.
It is not clear
> what your main program should be doing at this point, but an infinite loop
is a really
> losing idea since all it will do is suck down CPU cycles without doing
anything useful.
> You might consider waiting for the UI thread to terminate, but creating
CAsyncSocket in
> console apps is very unnatural. What do you have against GUI apps?
> joe

> On Wed, 6 Nov 2002 21:38:23 -0500, "Martin Mladenovski"

> >    Hi!

> >    I created a class CMySocket that inherits CAsyncSocket and overrides
> >OnAccept and OnReceive methods. In a console application that supports
MFC I
> >created a object from this class and called the methods

> >    myobj->Create(64000);    //port = 64000
> >    myobj->Listen(5);

> >    and after this I added and infinite loop, but nothing happens when a
> >connection is initialized with a client.

> >    How can I make in a console application with a MFC support, my
> >application to be interrupted when the listening socket accepts a
connection
> >or receives data (using CAsyncSocket or CSocket)?

> >    Best regards,
> >        Martin

> Joseph M. Newcomer [MVP]

> Web: http://www3.pgh.net/~newcomer
> MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm



Tue, 26 Apr 2005 00:53:49 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support

    Thank you Scott for explaining me the functionality of these two
classes,

        Martin


Quote:

> >     Hi!

> >     I created a class CMySocket that inherits CAsyncSocket and overrides
> > OnAccept and OnReceive methods. In a console application that supports
MFC I
> > created a object from this class and called the methods

> >     myobj->Create(64000);    //port = 64000
> >     myobj->Listen(5);

> >     and after this I added and infinite loop, but nothing happens when a
> > connection is initialized with a client.

> >     How can I make in a console application with a MFC support, my
> > application to be interrupted when the listening socket accepts a
connection
> > or receives data (using CAsyncSocket or CSocket)?

> >     Best regards,
> >         Martin

> The CAsyncSocket class uses the MFC message pump to receive the
> notifications.  This pump does not usually run in a console app.  (It
> also does not run in a windowed app while your code is in an infinite
> loop.)  CSocket has the same requirements.  The idea of an asynchronous
> socket is to notify an event-driven program when the socket needs
> attention.  But a console app is not event-driven.  Probably the easiest
> solution is to use a synchronous socket with winsock calls instead of
> MFC calls.

> --
> Scott McPhillips [VC++ MVP]



Tue, 26 Apr 2005 00:55:28 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support
Infinite loops do something. They do not listen or react to asynchronous connections.
There is no mechanism in a console app for this to happen.

Yes, most classes are event-driven. Not surprisingly, asynchronous I/O typically is
handled by event-driven models. Don't know what you mean by "would not have achieved
anything"--in fact, you would accomplish getting a working program.

Java hides all this event-driven stuff with a massively complex runtime, and has no form
of asynchronous I/O (at least the last time I looked at it, a year ago), which is a real
pain.

If you are trying to write a console app, you have to recreate all that runtime mechanism
that is built into Java. Which is why I tend to avoid writing console apps except in rare
situations, or when teaching my system programming course, which was designed to cover
only simple solutions the students can program in two-hour labs (my rule-of-thumb is that
if I can write it in 15 minutes, it makes a great 2-hour lab). I can't write asynchronous
sockets in a console app in 15 minutes. Or several hours. I always tell my students that
if they need to write an asynchronous socket handler, either (a) use CAsyncSocket, or (b)
look at third-part socket libraries, such as Dundas, SocketTools, or StingRay, (I know
nothing about these libraries except at some point I have looked briefly at them, so I
neither recommend nor endorse them--and there may be others I'm not aware of)
                                joe

Quote:

>    The code that I wrote was a simple example (not real code). But I needed
>a TCP server in
>a console application, that will go in an infinite loop and also do
>something in that loop besides waiting for a connection.
>    It seems like every class in MFC is an event driven class. I thought of
>using CWinThread, but also I would have not achieved anything.

>    In Java it is so simple if I use threads in a console application.

>    I managed to do this with a nonblocking sockets using Berkley's socket
>routines.

>    Thank you for your time,
>        Martin



>> Why 5? Why not SO_MAXCONN? Also, you should avoid hard-wiring port numbers
>whenever
>> possible.

>> What I don't understand is why you would go into an "infinite loop"? This
>guarantees that
>> you will get no events.

>> In a console app, you can't use this unless you create a UI thread to
>handle the socket
>> communication, because you need a message pump to make CAsyncSocket work.
>It is not clear
>> what your main program should be doing at this point, but an infinite loop
>is a really
>> losing idea since all it will do is suck down CPU cycles without doing
>anything useful.
>> You might consider waiting for the UI thread to terminate, but creating
>CAsyncSocket in
>> console apps is very unnatural. What do you have against GUI apps?
>> joe

>> On Wed, 6 Nov 2002 21:38:23 -0500, "Martin Mladenovski"

>> >    Hi!

>> >    I created a class CMySocket that inherits CAsyncSocket and overrides
>> >OnAccept and OnReceive methods. In a console application that supports
>MFC I
>> >created a object from this class and called the methods

>> >    myobj->Create(64000);    //port = 64000
>> >    myobj->Listen(5);

>> >    and after this I added and infinite loop, but nothing happens when a
>> >connection is initialized with a client.

>> >    How can I make in a console application with a MFC support, my
>> >application to be interrupted when the listening socket accepts a
>connection
>> >or receives data (using CAsyncSocket or CSocket)?

>> >    Best regards,
>> >        Martin

>> Joseph M. Newcomer [MVP]

>> Web: http://www3.pgh.net/~newcomer
>> MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Tue, 26 Apr 2005 07:41:30 GMT  
 CAsyncSocket or CSocket example in a console application with MFC support

    Thank you very much Joseph.

    I managed to do my console nonblocking TCP server with nonblocking
sockets in winsock2.h. It works perfectly. Also I've managed to put it as
simulink block in MATLAB.

    Thanks for your help,
        Martin



Quote:
> Infinite loops do something. They do not listen or react to asynchronous
connections.
> There is no mechanism in a console app for this to happen.

> Yes, most classes are event-driven. Not surprisingly, asynchronous I/O
typically is
> handled by event-driven models. Don't know what you mean by "would not
have achieved
> anything"--in fact, you would accomplish getting a working program.

> Java hides all this event-driven stuff with a massively complex runtime,
and has no form
> of asynchronous I/O (at least the last time I looked at it, a year ago),
which is a real
> pain.

> If you are trying to write a console app, you have to recreate all that
runtime mechanism
> that is built into Java. Which is why I tend to avoid writing console apps
except in rare
> situations, or when teaching my system programming course, which was
designed to cover
> only simple solutions the students can program in two-hour labs (my

rule-of-thumb is that
Quote:
> if I can write it in 15 minutes, it makes a great 2-hour lab). I can't
write asynchronous
> sockets in a console app in 15 minutes. Or several hours. I always tell my
students that
> if they need to write an asynchronous socket handler, either (a) use

CAsyncSocket, or (b)

- Show quoted text -

Quote:
> look at third-part socket libraries, such as Dundas, SocketTools, or
StingRay, (I know
> nothing about these libraries except at some point I have looked briefly
at them, so I
> neither recommend nor endorse them--and there may be others I'm not aware
of)
> joe
> On Thu, 7 Nov 2002 18:53:49 -0500, "Martin Mladenovski"

> >    The code that I wrote was a simple example (not real code). But I
needed
> >a TCP server in
> >a console application, that will go in an infinite loop and also do
> >something in that loop besides waiting for a connection.
> >    It seems like every class in MFC is an event driven class. I thought
of
> >using CWinThread, but also I would have not achieved anything.

> >    In Java it is so simple if I use threads in a console application.

> >    I managed to do this with a nonblocking sockets using Berkley's
socket
> >routines.

> >    Thank you for your time,
> >        Martin



> >> Why 5? Why not SO_MAXCONN? Also, you should avoid hard-wiring port
numbers
> >whenever
> >> possible.

> >> What I don't understand is why you would go into an "infinite loop"?
This
> >guarantees that
> >> you will get no events.

> >> In a console app, you can't use this unless you create a UI thread to
> >handle the socket
> >> communication, because you need a message pump to make CAsyncSocket
work.
> >It is not clear
> >> what your main program should be doing at this point, but an infinite
loop
> >is a really
> >> losing idea since all it will do is suck down CPU cycles without doing
> >anything useful.
> >> You might consider waiting for the UI thread to terminate, but creating
> >CAsyncSocket in
> >> console apps is very unnatural. What do you have against GUI apps?
> >> joe

> >> On Wed, 6 Nov 2002 21:38:23 -0500, "Martin Mladenovski"

> >> >    Hi!

> >> >    I created a class CMySocket that inherits CAsyncSocket and
overrides
> >> >OnAccept and OnReceive methods. In a console application that supports
> >MFC I
> >> >created a object from this class and called the methods

> >> >    myobj->Create(64000);    //port = 64000
> >> >    myobj->Listen(5);

> >> >    and after this I added and infinite loop, but nothing happens when
a
> >> >connection is initialized with a client.

> >> >    How can I make in a console application with a MFC support, my
> >> >application to be interrupted when the listening socket accepts a
> >connection
> >> >or receives data (using CAsyncSocket or CSocket)?

> >> >    Best regards,
> >> >        Martin

> >> Joseph M. Newcomer [MVP]

> >> Web: http://www3.pgh.net/~newcomer
> >> MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

> Joseph M. Newcomer [MVP]

> Web: http://www3.pgh.net/~newcomer
> MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm



Wed, 27 Apr 2005 19:15:28 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Console applications, MFC, and CSocket

2. Console support from MFC application

3. MESSAGE_MAP in combination with Win32 console application with MFC support

4. Console support from MFC application

5. Overriding CAsyncSocket OnReceive doesn't work in Win32 Console Application

6. CAsyncSocket in an MFC DLL used in a non-MFC Application

7. CAsyncSocket implementaion in C++ Console application

8. USING MFC-SocketInterface(CSOcket, Casyncsocket) in an ISAPI

9. CSocket in console application?

10. Using CSocket in a console application

11. CSocket with console application?

12. CSocket in a console application

 

 
Powered by phpBB® Forum Software