GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jun-2006, 08:24
draggy draggy is offline
New Member
 
Join Date: Jun 2006
Posts: 6
draggy is on a distinguished road
Exclamation

select() fds_write and exception source code example needed


hi......

Need tutorial or source code example based on how to use fds_write and fds_exception in select() function...


Thanks
  #2  
Old 22-Jun-2006, 09:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by draggy

Need tutorial or source code example based on how to use ... select() function

If man select() doesn't give enough information, then you can look here: Beej's network info site. (Actually that would be my first choice. I downloaded the latest version of the pdf file --- November, 2005 --- and I wouldn't even think of starting a networking project without it.)

Look up select() and its parameters.

If you write some code that doesn't seem to do what you want, you can post it here and describe what you are trying to do and describe what you don't understand about why it doesn't.

Regards,

Dave
  #3  
Old 22-Jun-2006, 09:31
draggy draggy is offline
New Member
 
Join Date: Jun 2006
Posts: 6
draggy is on a distinguished road

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by davekw7x
If man select() doesn't give enough information, then you can look here: http://beej.us/guide/bgnet/. (Actually that would be my first choice. I downloaded the latest version of the pdf file --- November, 2005 --- and I wouldn't even think of starting a networking project without it.)

Look up select() and its parameters.

If you write some code that doesn't seem to do what you want, you can post it here and describe what you are trying to do and describe what you don't understand about why it doesn't.

Regards,

Dave

Thank for reply........

I need know how to use fds_write and fds_exception in select()...

The tutorial only show fds_read...........
  #4  
Old 22-Jun-2006, 09:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by draggy
Thank for reply........

I need know how to use fds_write and fds_exception in select()...

The tutorial only show fds_read...........

So: I respectfully suggest that you write a program that calls select() with a non-NULL value for &readfds. Test it. Actually writing and testing a program (even if the fancy part id copied directly from some place else) is the best way (in my opinion) to learn.


If you can't use the information you get from actually using select() with readfsd to figure out how to use writefsd, show us what you have so far. Maybe someone can help.

Beej has done the "heavy lifting" (What a great effort: Thank you Beej!!!). Now it's your turn.


Regards,

Dave
  #5  
Old 22-Jun-2006, 11:06
draggy draggy is offline
New Member
 
Join Date: Jun 2006
Posts: 6
draggy is on a distinguished road

Re: select() fds_write and exception source code example needed


well then:
CPP / C++ / C Code:

for(;;)
{
       iTest = select(mafd +1, NULL, &write, &exception, NULL);

       if(FD_ISSET(listen, &write))
       {
             //write
             printf("Write\n");
       } 
       
        if(FD_ISSET(listen, &exception))
        {
             //exception
             printf("Exception occur\n");
        }
}

will the code above work?

if yes, how to invoke fds_write and fds_exception so I'm able to see the end result...

Thanks
Last edited by LuciWiz : 22-Jun-2006 at 14:00. Reason: Please insert your C code between [c] & [/c] tags
  #6  
Old 22-Jun-2006, 13:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by draggy
well then:

will the code above work?

Assuming that "listen" is the fd of the socket and that "write" and "exception" are fd_sets that have been associated with that socket (with FD_ZERO AND FD_SET), then the expression in the first if statement has a non-zero value ("true") if that socket can accept a "write" without blocking.

The expression in the second if statement has a non-zero value if that socket has experienced an exception.

Quote:
Originally Posted by draggy
how to invoke fds_write and fds_exception

I don't know what those functions are.


Regards,

Dave
Last edited by davekw7x : 22-Jun-2006 at 14:04.
  #7  
Old 22-Jun-2006, 14:09
draggy draggy is offline
New Member
 
Join Date: Jun 2006
Posts: 6
draggy is on a distinguished road

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by davekw7x
Assuming that "listen" is the fd of the socket and that "write" and "exception" are fd_sets that have been associated with that socket (with FD_ZERO AND FD_SET), then the expression in the first if statement has a non-zero value ("true") if that socket can accept a "write" without blocking.

The expression in the second if statement has a non-zero value if that socket has experienced an exception.


I don't know what those functions are.


Regards,

Dave

Thank for reply....

Code:
int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

I mean anyway to test or invoke those fd_set *writefds and fd_set *exceptfds............. cause is hard to know the program is working the correct way........

another thing according to Beej's Guide to Network Programming Using Internet Sockets here:

http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html#select

Code:
// run through the existing connections looking for data to read for(i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &read_fds)) { // we got one!! if (i == listener) { // handle new connections } } }

that mean we checking through " i " of sockets?

Thanks
  #8  
Old 22-Jun-2006, 17:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by draggy
cause is hard to know the program is working the correct
I totally agree: it's hard to get network stuff running without actually testing it on a network. It's pretty much impossible to understand what will happen without actually seeing it happen (for me at least).

Quote:
Originally Posted by draggy
another thing according to Beej's Guide to Network Programming Using Internet Sockets here:

http://beej.us/guide/bgnet/output/ht...et.html#select

Code:
// run through the existing connections looking for data to read for(i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &read_fds)) { // we got one!! if (i == listener) { // handle new connections } } }

that mean we checking through " i " of sockets?
Yes. After the socket is opened and bound to port 4093, there is a big loop that maintains and services the list of fd's for clients. Initially the list contains only the fd that was bound to the port (this is the "listener" port); new connections are detected when FD_ISSET indicates that the listener port has some data

Now here's the big loop: repeat 1, 2, 3 forever:

1. Does a select() and sees if there is anything to be read from any of the ports in the list of fd's(by using FD_ISSET).

2. If there is, it checks to see if this port is the "listener" port (which indicates a telnet message). If it is, then FD_SET tells it whether it is a new user (by returning a new fd that is bigger than anything in the list). New clients are added the the list by FD_SET.

3. If FD_ISSET reports something other than the "listener" port, it must be from a client, so instructions are executed to get the message from the fd who reported data available and then another loop sends the message to all of the other client fd's in the list.

Voila: a chat server for telnet clients! Any message typed by a connected client is broadcast to all of the connectees.

(A few more details by Beej indicate how a request for connection from a new client is detected and how the server knows when a client has closed a connection. The server maintains the list of fd's with FD_SET and FD_CLEAR.)


Regards,

Dave
  #9  
Old 23-Jun-2006, 07:49
draggy draggy is offline
New Member
 
Join Date: Jun 2006
Posts: 6
draggy is on a distinguished road

Re: select() fds_write and exception source code example needed


well, basically I want select() to throw either write or exception file descriptor...

which means enable the program to test either:

1) if(FD_ISSET(listen, &write))
2) if(FD_ISSET(listen, &exception))

so anyway?

Thanks
  #10  
Old 23-Jun-2006, 09:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: select() fds_write and exception source code example needed


Quote:
Originally Posted by draggy
well, basically I want select() to throw either write or exception file descriptor...
I don't know what you mean by this. The concept of "throwing an exception" is no part of any select() function that I have ever used. If you are using some C++ classes with names similar to the traditional network functions, then we have wasted a lot of board bandwidth, since Beej and I are using C glibc library functions that are included with Linux and some other distributions. (cygwin on Windows, for example).

Use of the word "exception" in this context implies one of a small number of "exceptional" events (out-of-band data received on the port, or some such thing).

Note that if a telnet client quits, this does not generate an exception on its port. This is not an exceptional event; it is a normal telnet command. If you actually compile and run the Beej example you will see that the "hang up" condition is detected when the read() command returns zero bytes. (Have you done that? You really need to compile and execute ---and, I would hope, understand--- something that works before you try to go beyond the example.)

I'll try to spell things out to the best of my understanding.

When you call select:

CPP / C++ / C Code:
    select(maxfdp1, &readset, &writeset, &exceptset,&timeout)

it sets status bits whose states can be interrogated by FD_ISSET. Any or all of the arguments &readset, &writeset, &exceptset can be NULL (so you won't be testing that condition for any of the fd's in that set).

Beej's example, as you have noted, only needs readset. If you have more than one non-null argument, then you must have different fd_sets each fd_set is created like this
CPP / C++ / C Code:
    fd_set fdset_whatever;
.
.
.
    FD_ZERO(fdset_whatever);

and each of the fd_sets has certain fd's as members. Members are added to an fs_set with FD_SET:

CPP / C++ / C Code:
    FD_SET(fdnumber_whatever, &fdset_whatever);



If "listen" is an fd that has been added to readset (with FD_SET(listen, &readset)) Then if you execute the above select() function, you can tell whether a read from the "listen" fd can proceed without blocking.

CPP / C++ / C Code:
    if (IS_SET(listen, &readset)) {
        /* you can read from "listen" without blocking */
    {
    else {
        /* a read from "listen" will block */
    }

If "listen" is an fd that has been added to writeset (with FD_SET(listen, &writeset)) Then if you execute the above select() function, you can tell whether a write to the "listen" fd can proceed without blocking:
CPP / C++ / C Code:
    if (IS_SET(listen, &writeset)) {
        /* you can write to "listen" without blocking */
    {
    else {
        /* a write to "listen" will block */
    }


If "listen" is an fd that has been added to exceptset (with FD_SET(listen, &exceptset)) Then if you execute the above select() function, you can tell whether there was an exceptional event on "listen" fd:

CPP / C++ / C Code:
    if (IS_SET(listen, &exceptset)) {
        /* an exception has occurred on "listen"  */
    {
    else {
        /* an exception has not occurred on "listen" */
    }



Regards,

Dave
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.