Thread: non blocking operation

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    non blocking operation

    hi, i'm new on the C/C++ scene.
    i want to make a socket listen & accept procedure during getch()
    the problem is that both of them are blocking methods.
    i know about select function, but i didn't manage to get it work (maybe because getch() is blocking it)

    the idea is to make UI to the user, and simultaniously accept remote connections.

    Code:
    ...
    ...
    void listener(int port) {
         struct timeval tv = { 0L, 0L };
         fd_set acc;
         int s,siz;
         struct sockaddr_in m,remote;
         s = socket(AF_INET,SOCK_STREAM,0);
         m.sin_family = AF_INET;   
         m.sin_port = htons(port);
         m.sin_addr.s_addr = INADDR_ANY; 
         memset(&(m.sin_zero), '\0', 8);  
         printf("asd");     
         bind(s, (struct sockaddr *)&m, sizeof(struct sockaddr));
         listen(s,5);
    
         FD_ZERO(&acc);
         FD_SET(s, &acc);
    
         printf("%d",select(1, &acc, NULL, NULL, &tv)); // tried to debug and see the output, always equals 0
    
         while(!select(1, &acc, NULL, NULL, &tv)) {
         siz=sizeof(struct sockaddr); 
    
         accept(s, (struct sockaddr *)&remote, &siz); 
         printf("accepted connection from %s",inet_ntoa(remote.sin_addr));
    
           }
         
         }
    ...
    ...
    int main() {
    ...
    ...
     x:         listener(123);
                        printf("Press computer number to perform tasks, r to check connections or q to quit.\n");
                        
             inp = getch();
             switch(inp) {
    ...
    ...
    ...
     goto x;
    ...
    }
    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could add "stdin" as one of the select set - and then check if it was your socket or stdin that got ready.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    i've tried...

    stdin is 0 file descriptor, isn't it?
    the select returns error (-1), and WSA error is 10038 (invalid socket).
    i'm working under windows XP SP2.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Alon View Post
    stdin is 0 file descriptor, isn't it?
    the select returns error (-1), and WSA error is 10038 (invalid socket).
    i'm working under windows XP SP2.
    Ah, ok, I though you used Linux - in Windows, you will have to do "somthing else". A non-blocking keyboard read will be what you want. I'm pretty sure there is such a thing in the console functions somewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yeah, windows broken select() only works on actual socket fd's, not any valid fd in the system.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i=++i; operation undefined?
    By password636 in forum C Programming
    Replies: 10
    Last Post: 04-16-2009, 09:46 AM
  2. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 03:17 PM
  3. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  4. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM