accept()

This is a discussion on accept() within the Networking/Device Communication forums, part of the General Programming Boards category; I am trying to write a wrapper class for simplicity around sockets so they are easily portable. I know accept() ...

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    accept()

    I am trying to write a wrapper class for simplicity around sockets so they are easily portable. I know accept() waits untill you connect() from the client, but is there a way to check if there is something to accept() before calling accept()?

    An analogy I don't wait with my front door open untill my friend shows up I wait for the doorbell before I open the door and go about my business then go answer the door.

    I mean there must be some way to check or maybe timeout accept() right? I would prefer not to use threads as I am not familiar with them fork() seems to not share data across threads - I'm not sure if globals are still accessible by all or not in that case. If there is a way to check before accept()ing that would be a big help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ~Kyo~ View Post
    I know accept() waits untill you connect() from the client, but is there a way to check if there is something to accept() before calling accept()?
    select()
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Then ideally:
    Code:
    bool MySocket::HasData()              
    {
      timeval tv;   
      tv.tv_sec = 0;
      tv.tv_usec = 10000;
      fd_set readset;
      FD_ZERO(&readset);
      FD_SET(fd, &readset);
      int result = select(fd, &readset, NULL, NULL, &tv);
      if(result > 0) //Has data!
      {
        cout<<"Y";        
        return true;  
      }else return false; //No Data!
    }
    would check if I need to accept() or not? So it seems spliced some code together didn't know I could select() something that hasn't been accept()ed should work thanks;
    Last edited by ~Kyo~; 11-22-2009 at 11:04 PM.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by abachler View Post
    Tch.

    Quote Originally Posted by ~Kyo~ View Post
    I am trying to write a wrapper class for simplicity around sockets so they are easily portable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by mk27 View Post
    tch.
    wine.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I have been writing some code to overload the = operator as far as I know the only things I need to keep constant to transfer control between two instances of the class are:

    SOCKET FD; I could use an int from what I understand.
    sockaddr client;
    timeval tv; I probably don't need to copy this since I set its values before I use them in the function

    Currently my code reads as this.
    Code:
    MySocket MySocket::operator= (const MySocket &tmp)
    {
       client = tmp.client;      
       fd = tmp.fd;                     
       tv = tmp.tv;       
      return *this;         
    }
    The catch is I want to make a MySocket bind() and listen() then accept() then pass it off to another MySocket inside an array of MySockets - I know they have code to do something similar with max_fd+1 or something I can't read that code and say ahh yes thats what they were thinking what ever happened to C++ being readable by human eyes and not looking nasty?

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Well, just my $0.02, but I would implement CServer, not CSocket, as then you can wrap the full functionality fo accept() into teh class, without having to pass it off to some odd ball overloaded version of the same class.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    ... I am only overloading the assignment op not the whole class basicly I have written some functions to fix some issues you get with lets say send() for all I know send() might send only part of a message and in my case I need to make sure the whole message sends every time. I can send a 1000000 char string probably and it will send() in once function it is also more portable than having these vauge descriptions that whoever desgin this stuff tends to use.

    Maybe an example... this first part isn't my code it is some code I have been looking at on the net. For whatever reason it isn't indented - which makes it even worse to read, but ehh.
    Code:
    int myfds[N], maxfd, j;
    fd_set readset;
    ...
    // Initialize the set
    FD_ZERO(&readset);
    maxfd = 0;
    for (j=0; j<N; j++) {
    FD_SET(myfds[j], &readset);
    maxfd = (maxfd>myfds[j])?maxfd:myfds[j];
    }
    
    // Now, check for readability
    result = select(maxfd+1, &readset, NULL, NULL, NULL);
    if (result == -1) {
    // Some error...
    }
    else {
    for (j=0; j<N; j++) {
    if (FD_ISSET(myfds[j], &readset)) {
    // myfds[j] is readable
    }
    }
    }
    Now my code looks simpler:
    Lets say I want to make a socket and listen on it for a connection then send a string out when the connection is open.
    Code:
    int main(int argc, char *argv[])
    {
       ininetwork(); //does my WSAStartup call
       MySocket First;
       First.prep();  //socket() call with params
       if(-1 == First.Bind(PORT)) //PORT is a #define 3490 bind also has my listen code as well
       {
          //error code
       }
       First.Accept();  //waits for a connection
       First.Send("This is a string");
       First.Close();
       First.ShutdownAll();   //WSACleanup()
    }
    I would much rather prep bind accept send/recv close shutdown.

    Than make data types that are vauge and some might be outdated or not or yes or who knows copy and paste code over and over that should be in a function - which to me if it is possible use a class thats what C++'s main strength tends to be. I mean I don't need to remember that params 4 and 5 are null in some call. To connect I pass an IP and a Port and I am good SOCK_STREAM and tcp-ip the normal stuff is ready for me.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    What I'm trying to tell you is that a class that only handles sockets will be much less portable than one that handles the entire client/server. Especially for the server side, once you start handling more than a few connections, you will find that you can no longer do things like thread per connection or connection polling, as they are not responsive enough.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    By connection polling you mean something like:

    Code:
    for(int x=0;x<MAX_CON;x++)
    {
       if(connections[x].HasData()) // do something set a flag etc.
    }
    I have noticed latancy in a game while using that methodology. I don't need super fast response times with a few programs I am writing at the moment as they do some simple things like keep an inventory of parts with keywords then shoot everything that fits the description back to the end user which displays the data. Or something smaller scale I will be writing a small app to keep up with my companies taxes - yes I could buy quickbooks, but I want to put my own functionality into the app. Something like this isn't too bad to use in these cases maybe if I want to write the next WoW like MMO I would need to use something more powerful in comparison as response time is extra important in those situations.

    I will look at the Cserver stuff - what headers/libs do I need to find since I use bloodshed dev C++ not MSVC++

  12. #12
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not sure I'd use select(). For one, the man page notes:
    Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.
    ...and thus the advice to always use non-blocking sockets with select(). I'd give the same advice here, since that's really what you want: Mark the socket non-blocking, and forget select. Just call accept(). If you get EWOULDBLOCK, then obviously there's nobody there.

    As a side note, fork() doesn't create threads, it creates processes.
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a single socket for accept() as well as connect()?
    By nkhambal in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-20-2005, 06:43 AM
  2. accept() Hangs
    By Epo in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-09-2005, 12:53 PM
  3. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 05:59 PM
  4. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 06:50 PM
  5. How to use -- scanf to accept space?
    By leena_teoh in forum C Programming
    Replies: 4
    Last Post: 02-07-2002, 11:22 AM

Tags for this Thread


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