program structure with select sockets

This is a discussion on program structure with select sockets within the Networking/Device Communication forums, part of the General Programming Boards category; I've learned how to use blocking sockets, but while moving into non-blocking sockets, I've gotten kind of confused. It seems ...

  1. #1
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67

    program structure with select sockets

    I've learned how to use blocking sockets, but while moving into non-blocking sockets, I've gotten kind of confused. It seems the program has to be entirely restructured, and the examples I've seen have a message loop.
    1. Can you use select sockets in command line?
    2. Can you have a message loop in the command line?
    3. Could someone explain to me how a select socket program is structured, and how to use threading?

    thanks.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  2. #2
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    1. Yes you can use select in command line based programs.

    2. Of course you can have a message loop, it will appear with each select timeout.

    3. Please look at a simple server example in the BeeJ's guide: general network question
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  3. #3
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    yeah, i've looked at that, I've tried my hardest not to be an ignorant ass. The thing is, Beej's code looks like it's written to work best on a unix system, and it makes following his examples kind of tough on dev-c++ on windows.

    I understand the concept, but the only example i've seen that i can understand is written as a windows window, not a command line project, using the message loop to handle select() returning a value. I just want to do it in command line though, and I've never seen a command line program with a message loop, so I've just been kinda stumped.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I understand your problem, but you need to understand, that Microsoft didn't create socket programming, it's just the same.
    I believe the guide contains a section explaining how to make the functions to work under windows, please read it!

    But any way, the examples work, trust me on that one, the only thing that could be a problem for windows is fork() which is nothing more than a new child process "creator", but is not supported by windows.


    As for a command line program, I can't believe you did API before simple command like applications.

    Just remember to keep looping, set a timeout timer for select and avoid getting to the blocking functions, unless you have incoming data which will not make the functions to block. And each time select timeouts display a message.
    Go through the example again; As far as I remember it works on windows.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    This was an example of the implementation of select() in Beej's code;

    Code:
    /*
    ** select.c -- a select() demo
    */
    
    #include <stdio.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #define STDIN 0  // file descriptor for standard input
    
    int main(void)
    {
        struct timeval tv;
        fd_set readfds;
    
        tv.tv_sec = 2;
        tv.tv_usec = 500000;
    
        FD_ZERO(&readfds);
        FD_SET(STDIN, &readfds);
    
        // don't care about writefds and exceptfds:
        select(STDIN+1, &readfds, NULL, NULL, &tv);
    
        if (FD_ISSET(STDIN, &readfds))
            printf("A key was pressed!\n");
        else
            printf("Timed out.\n");
    
        return 0;
    }
    I don't like to copy code, so i tried to write my own code from looking at that, but it didn't work, so i tried copying the code, and that didn't work either, my compiler doesn't recognize any of the "FD" commands. this is all the code, and it was linked to w2_32.lib.

    Edit: because it seems that those FD commands are all over his code, i got discouraged and gave up on his tutorial.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    The FD comands are located in winsock.h and winsock2.h - which one you include depends on what you link with. You mentioned "w2_32.lib" - are you using Dev-C++ or MSVC? Dev-C++ would add -lwsock32 or -lws_32 as parameters when linking.

    Also, I'm not sure whether select() will work on anything other than sockets in Windows. (You can always find out, of course) Also: Most of the socket functions will fail (at least when working with sockets) until you've started Winsock: see WSAStartup().

    Hope that helps!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    alright, now i've fixed the link problem, but you might be onto something with the select thing only being useable on sockets in windows. that code simply produced "a key was pressed!" every time for me, even if i put a system("PAUSE") at the end. i'll look into it more tomorrow, but thanks for helping me get something out of that code.

    Edit: you're probably right about another point too, that all the winsock functions will fail until winsock is started. when i wrote my blocking sockets server, i had to initialize winsock, otherwise everything would fail. this code has none of that, so i guess it's not the same on unix.

    is this code really the best source for me?
    Last edited by n3v; 06-02-2006 at 01:05 PM.
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    1. I meant the server example.
    2. You didn't read the section on porting to windows.

    And yes! I really think this is the best source for you for socket programming
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    n3v
    n3v is offline
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    which header files are used for unix, and can be removed?
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    You can remove all the headers listed in all the examples... except stdio.h and like it which are part of the ANSI C language.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Simple Structure program
    By dopejack in forum C Programming
    Replies: 19
    Last Post: 12-15-2007, 09:30 AM
  3. Sockets program
    By athos in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-14-2004, 02:46 PM
  4. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM
  5. structure program
    By prlove71 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 08:01 PM

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