Simple telnet client demonstration with boost asio asynchronous I/O

I've been teaching myself how to use the asynchronous I/O capabilities in Boost asio. I took the chat client example code that came with Boost and modified it to act as a more general telnet client. Hopefully, this may be of use to others trying to get to grips with the basics of this way of working. More experienced developers can feel free to suggest better ways of doing things here. This is just a demonstration - it does not do all the niceties of a full application, but hopefully covers the basics. I wrote this for a Linux system, but the only system specific thing is the code that enables single keypress input for cin.get(). I'm sure you can easily find alternatives for your system to achieve the same result. Enjoy, Jeff

Hi Jeff, thanks, I am going to dive into Boost::Asio myself, soon, and will come back to your example then :-) Just wondering (while being aware, that this is not a fully fledged telnet client): I tried to connect to localhost, port 23. There is no service running, so I would have expected the program to terminate (it terminates when I specify a host which does not exist). I even sent 800MB of XML data into the program. It did not complain and wrote it to Nirvana, I guess :-) Regards, Roland Jeff Gray wrote:
I've been teaching myself how to use the asynchronous I/O capabilities in Boost asio. I took the chat client example code that came with Boost and modified it to act as a more general telnet client.
Hopefully, this may be of use to others trying to get to grips with the basics of this way of working. More experienced developers can feel free to suggest better ways of doing things here.
This is just a demonstration - it does not do all the niceties of a full application, but hopefully covers the basics.
I wrote this for a Linux system, but the only system specific thing is the code that enables single keypress input for cin.get(). I'm sure you can easily find alternatives for your system to achieve the same result.
Enjoy, Jeff
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Roland Bock wrote:
thanks, I am going to dive into Boost::Asio myself, soon, and will come back to your example then :-)
Just wondering (while being aware, that this is not a fully fledged telnet client): I tried to connect to localhost, port 23. There is no service running, so I would have expected the program to terminate (it terminates when I specify a host which does not exist). I even sent 800MB of XML data into the program. It did not complain and wrote it to Nirvana, I guess :-)
You're quite right - I haven't tested this code for failure like this. My initial thought is that it probably silently calls the close function and exits the communication thread without exiting the application. I'll look at adding this resubmitting the code - it should be a straightforward change. Jeff ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________

Here is a CMakeLists.txt file that will let you use CMake
(http://www.cmake.org) to create either a visual studio solution or a
kdevelop/unix makefile solution. Your code builds with no errors using
Visual Studio 2008 and (of course) Linux (Debian), I haven't tried a
mac.
Andrerw
On Tue, Sep 30, 2008 at 10:51 AM, Jeff Gray
Roland Bock wrote:
thanks, I am going to dive into Boost::Asio myself, soon, and will come back to your example then :-)
Just wondering (while being aware, that this is not a fully fledged telnet client): I tried to connect to localhost, port 23. There is no service running, so I would have expected the program to terminate (it terminates when I specify a host which does not exist). I even sent 800MB of XML data into the program. It did not complain and wrote it to Nirvana, I guess :-)
You're quite right - I haven't tested this code for failure like this. My initial thought is that it probably silently calls the close function and exits the communication thread without exiting the application.
I'll look at adding this resubmitting the code - it should be a straightforward change.
Jeff
______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ___________________________________________ Andrew J. P. Maclean Centre for Autonomous Systems The Rose Street Building J04 The University of Sydney 2006 NSW AUSTRALIA Ph: +61 2 9351 3283 Fax: +61 2 9351 7474 URL: http://www.acfr.usyd.edu.au/ ___________________________________________

Andrew Maclean wrote:
Here is a CMakeLists.txt file that will let you use CMake (http://www.cmake.org) to create either a visual studio solution or a kdevelop/unix makefile solution. Your code builds with no errors using Visual Studio 2008 and (of course) Linux (Debian), I haven't tried a mac.
Thanks for that Andrew. Nice to see an easy portable build system. I've worked on Roland Bock's suggestion of timeouts to handle failure to connect & attached the working code. When an error occurs in the class, in my example code, it sets a variable which is checked by the main thread. I'm sure there are better built in ways of dealing with this for Boost asio or threads. Presumably, when the socket is closed, this means the io_service.run() function returns and the thread is terminated. If anyone has any suggestions how to detect any of this from the main thread to determine when to exit, I'd like to hear them. Regards, Jeff

It works nicely, this is a good example of using ASIO.
The CMakeLists.txt file could have been made more minimal but I hope
people find the extra bits useful as templates.
Regards
Andrew
On Tue, Sep 30, 2008 at 7:49 PM, Jeff Gray
Andrew Maclean wrote:
Here is a CMakeLists.txt file that will let you use CMake (http://www.cmake.org) to create either a visual studio solution or a kdevelop/unix makefile solution. Your code builds with no errors using Visual Studio 2008 and (of course) Linux (Debian), I haven't tried a mac.
Thanks for that Andrew. Nice to see an easy portable build system.
I've worked on Roland Bock's suggestion of timeouts to handle failure to connect & attached the working code.
When an error occurs in the class, in my example code, it sets a variable which is checked by the main thread. I'm sure there are better built in ways of dealing with this for Boost asio or threads.
Presumably, when the socket is closed, this means the io_service.run() function returns and the thread is terminated. If anyone has any suggestions how to detect any of this from the main thread to determine when to exit, I'd like to hear them.
Regards, Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ___________________________________________ Andrew J. P. Maclean Centre for Autonomous Systems The Rose Street Building J04 The University of Sydney 2006 NSW AUSTRALIA Ph: +61 2 9351 3283 Fax: +61 2 9351 7474 URL: http://www.acfr.usyd.edu.au/ ___________________________________________
participants (3)
-
Andrew Maclean
-
Jeff Gray
-
Roland Bock