using select with sockets.

This is a discussion on using select with sockets. within the C Programming forums, part of the General Programming Boards category; exert from code: (just the essencial) Code: fd_set fdset,fdsetbck; FD_ZERO(&fdsetbck); FD_SET(sfd,&fdsetbck); (sfd is a tcp sockect, after listen instruction) while(1){ ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    using select with sockets. (solved)

    exert from code: (just the essencial)

    Code:
    fd_set fdset,fdsetbck;
    
    FD_ZERO(&fdsetbck);
    FD_SET(sfd,&fdsetbck);
    (sfd is a tcp sockect, after listen instruction)
    while(1){
     memcpy(&fdset,&fdsetbck,sizeof(fdsetbck));
    
     select(maxfd+1, &fdset, NULL, NULL, NULL);
    
     if (FD_ISSET(sfd,&fdset)){
     if((newfd=accept(sfd,(struct sockaddr*)&addrs,&addrlens))==-1) ....
     FD_SET(newfd,&fdsetbck);
    if (newfd>maxfd) maxfd=newfd;
    
     add newfd to descriptors array
     ...
    for cycle testing array of descriptors
     if (FD_ISSET(array[n],&fdset))
    }
    }
    my ideia is: each time there is a new connection on the socket a new file descriptor is created and added to the file set. that way if i get a message on the file descritpr used in listen or any of the ones created before i can read from it.

    the problem is: when i get the 1st connection on sfd, newfd is created and i can read from it, and add it to the fdset. when it enters 2 loop, the (FD_ISSET(sfd,&fdset)) condition is true again, when i know there was no connections on sfd.
    am understanding this wrong, of do i have to flush, or clean, or something like that, sfd, so that select doesnīt think there is a new connection on it?

    tks
    Last edited by Ironic; 04-02-2009 at 08:56 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's been a while since I've done this, but you should be calling select on your set of descriptors, not just on INT_MAX.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    iīve even tried the edited version of code above. same thing happens

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When in doubt, call Beej: Beej's Guide to Network Programming


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    problem solved. had a mistake somehere else

    tks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  2. program structure with select sockets
    By n3v in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-03-2006, 07:34 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM

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