Using Gtkmm with sockets

This is a discussion on Using Gtkmm with sockets within the C++ Programming forums, part of the General Programming Boards category; While I was programming a file receiver/sender , I was recommended not to use the select() function for reading active ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Using Gtkmm with sockets

    While I was programming a file receiver/sender , I was recommended not to use the select() function for reading active sockets, I was told to use event libraries such as libevent. I've read the documentation about those libraries and they use an event loop, what would you recomment to read and process active connections while gtkmm is in an idle status? Gtkmm also has a socket monitoring library, but I really don't like it. What do you recommend me to use?
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Did you ask why you should not use a select? It's my understanding that select is one of the more standard approaches to monitoring streams in general (sockets included).

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I was told that select has a poor performance on windows applications. I want to make the app for both linux and windows

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > I was told that select has a poor performance on windows applications
    Rather than being told everything, why not actually try some things and find out for yourself.

    Instead of worrying how "efficiently" a function that waits for things to happen (like select) actually is.

    You haven't written anything yet, but already you're premature optimising. It might be "poor" in relative terms (to other windows specific approaches), but in absolute terms for your actual program, it might not make a bean of difference.

    > I want to make the app for both linux and windows
    You've already sacrificed some performance by deciding to go for portability.

    > What do you recommend me to use?
    The thing between your ears.
    Look at the options you have, make some choices, do some experiments.

    So long as you have a half-decent design, then refactoring areas with poor performance (when you've got something to benchmark) will be relatively easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    If you're going for a multi-platform solution... I'd recommend wxWidgets to my own mother. It has the socket and event functionality you're looking for. And don't be fooled, wxWidgets applications may be console or GUI based. And yes, Windows' select is a poor hack-job that barely mimics the functionality of linux's, but it'll do, pig. It'll do.

    Side note: it's not tough to make a simple program (such as a file sender/receiver) portable. You probably won't have to change more than a few socket calls and includes.

    Bonus note!: If you're feeling adventurous and this program is more of a toy learning tool than a major project, you can always use #ifdefs and compiler options to make your program portable.

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #ifdef __WIN32__
    #include <winsock.h>
    #else
    #include <sys/socket.h>
    #include <sys/types.h>
    #endif
    
    int foobar() {
    #ifdef __WIN32__
    // LOL WSAStartup crap
    #endif
    }

  6. #6
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I'll take a look at wxWidgets, but gtrkmm also works multi platform. My main doubt was how to imitate the select function in the main loop of gtkmm, but I think I know how.
    How can I know when data is ready to be readen in a socket?
    Thanks for everything

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Quote Originally Posted by lautarox View Post
    I'll take a look at wxWidgets, but gtrkmm also works multi platform. My main doubt was how to imitate the select function in the main loop of gtkmm, but I think I know how.
    How can I know when data is ready to be readen in a socket?
    Thanks for everything
    I'm not sure how it's done in gtrkmm, but wxWidgets works with a callback, rather than select, kind of like:
    Code:
    void MyClass::MyCallback(wxSocketEvent& e) {
    
    switch e.EventType() {
    case SOME_DEFINE_CONNECT:
        // Do connect stuff
    case SOME_DEFINE_DISCONNECT:
        // Do disconnect stuff
    case SOME_DEFINE_RECV
        // Do recv stuff
    default:
    }
    
    }

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You may also want to look at glib, which I think is necessary for gtk, so you are already using it anyway:

    IO Channels

    That tish is perfect for use in event loops, it also uses callbacks -- eg, with g_io_add_watch(), you just set a function to fire when the socket has data waiting to read. It has additional functionality too, such as detecting when an error has occurred on the socket because the other end disappeared.

    I replaced a good hundred or so lines of code involving multiple sockets, a forked server, and a polling function in a gtk+ app when I found that gio_add_watch() thing.
    Last edited by MK27; 03-01-2010 at 07:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Also the glib is portable. Do you recommend me to use the glib? I was worried about windows performance..

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Gtk requires glib everywhere, inc. windows:

    GTK+ - Download for Windows

    Pretty sure that the event loop used in gtk is from glib, which has a ton of functionality if you start looking (inc the high level io stuff). I believe gtk and glib were both developed together years ago for GIMP.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Yeah, that's right, I've taken a look to the i/o monitoring functions Monitoring I/O I really don't know if it could handle much connections, but with 10 or 20 I think it will be ok. I think I'll use the monitoring function to receive the incoming data and the idle functions to send when there's nothing to do Idle Functions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 06:36 PM
  3. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  4. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM
  5. Sockets, questions.
    By Vber in forum C Programming
    Replies: 2
    Last Post: 12-25-2002, 11:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21