select

This is a discussion on select within the Networking/Device Communication forums, part of the General Programming Boards category; I have a program which opens about 100 sockets and sends/received data.... AT present I am using some sort of ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    select

    I have a program which opens about 100 sockets and sends/received data.... AT present I am using some sort of polling to see if there is data to be read and I poll the array to check if there is any data to be sent out...


    Now I understand that i can use select so that a lot of polling is eliminated... i.e. the select statement will return when a socket is ready for reading or for writing... But the problem is that I data is written ocationally and I dont want select to return untill there is data to be written....i.e. some data is found in the send array... I am presently using a loop to check this but is inefficient.. any suggestions..

    vasanth

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A loop is probably your best bet. Something like
    Code:
    select ( wait for 1 second to see if there's data to read )
    Process any sockets that select has said are readable
    send any data you need to 
    goto step 1
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Hammer
    A loop is probably your best bet. Something like
    Code:
    select ( wait for 1 second to see if there's data to read )
    Process any sockets that select has said are readable
    send any data you need to 
    goto step 1
    thanx .. that suggestion seems good...

    is it possible to write some kind of interrupts that interrupt when an array is filled with some data... i want to eliminate polling..

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is a non-blocking I/O model.

    Kuphryn

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by vasanth
    thanx .. that suggestion seems good...

    is it possible to write some kind of interrupts that interrupt when an array is filled with some data... i want to eliminate polling..
    Are you able to use window sockets? If you can only use unix sockets I don't know of any non poll method.

    Non blocking I/O models still use polling to determine what sockets to work on.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I am presently using non blocking sockets.. but still i need to poll them.. the problem was solved by using select call.. but I still have to poll the send buffer which in turn makes the select ineffective.. I cant use Hammers suggestion since sending data is time critical and cant wait for a given time if data is ready to be sent...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not sure how much it'll help but:
    I wrote a socket I/O class that is derived from the std::iostream class. It allows reading and writing from a socket just like cin/cout with all of its standard functions. Currently it doesn't work with UDP sockets.

    If you think it'll help take a look: http://www.mikemill.org/programming/sockio/index.php

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I cant use Hammers suggestion since sending data is time critical and cant wait for a given time if data is ready to be sent
    When you use select(), if your sockets are busy, which I guess they will be as you've got so many of them, the "1 second delay" will never actually be 1 second. select() will return as soon as sockets are ready, waiting up to the specified time period for this to be. If your sockets are busy, then chances are select() will return almost immediately, allowing your app to continue processing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Hammer
    >>I cant use Hammers suggestion since sending data is time critical and cant wait for a given time if data is ready to be sent
    When you use select(), if your sockets are busy, which I guess they will be as you've got so many of them, the "1 second delay" will never actually be 1 second. select() will return as soon as sockets are ready, waiting up to the specified time period for this to be. If your sockets are busy, then chances are select() will return almost immediately, allowing your app to continue processing.
    well thats the problem i am having.. i want to use select for reading and not writing.. but I dont want to wait to write wehn there is data in the array to be written... Using select to find if the socket is writable again translates into a poll since the sockets are almost always ready and the data i write to each of them is verry small... I am planing to have the socket read in a seperate thread so i can use select.. but not sure if the performance gained would be greater than the performance lost due to context switching of threads.....

  10. #10
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Thantos
    Not sure how much it'll help but:
    I wrote a socket I/O class that is derived from the std::iostream class. It allows reading and writing from a socket just like cin/cout with all of its standard functions. Currently it doesn't work with UDP sockets.

    If you think it'll help take a look: http://www.mikemill.org/programming/sockio/index.php
    hmm thanx... looks like I can use this in my next socket stuff... Makes it easier to read and write...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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