Error with accept();

This is a discussion on Error with accept(); within the Networking/Device Communication forums, part of the General Programming Boards category; I'm working my way through Beej's Tutorial, and have ran into a snag. Code: new_fd = accept(sockfd, (struct sockaddr *)&their_addr, ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Error with accept();

    I'm working my way through Beej's Tutorial, and have ran into a snag.

    Code:
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    this portion of code is throwing some errors I'm not aware of.

    invalid conversion from int* to socklen_t*
    initializing argument 3 of int accept(int, sockaddr*, socklen_t*)

    does anyone know why this is happening?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    I think the third parameter should be sizeof(their_addr)...Probably why it's complaining about incorrect datatypes - what type is sin_size?

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    ok, i ran the same code as a c program instead of c++ and it worked.
    does anyone know of a c++ version of beej's server.c?
    i like c, but i'm more fluent with c++, and i feel i would learn more from that code.
    do not get me wrong the c version has taught me a lot.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    i've compiled the same code as C++ (gcc 3.3.5) and it worked. What compiler are you using?
    what is sin_size type? should be socklen_t .
    Nazca, no the 3rd arg to accept is a value-result argument, a pointer.
    :wq

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You are passing an int* as the third parameter, as the error message fairly obviously states it wants a socklen_t. It needs a pointer to be because it changes its value in teh accept to tell you how many bytes were written. wither make sin_size a socklen_t, as it wants, or cast it to a socklen_t* in the call.

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I was using both VC++7 and GCC3.3.4 when I was having the problem.
    I found another tutorial that is helping me with c++ and winsock, but i was still unable to get this code to work as c++.
    here is the example code i'm using from beej's guide
    Code:
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
     
    #define MYPORT 3490 // the port users will be connecting to
     
    #define BACKLOG 10 // how many pending connections queue will hold
     
    main()
    {
    int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
    struct sockaddr_in my_addr; // my address information
    struct sockaddr_in their_addr; // connector's address information
    int sin_size;
     
    sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!
     
    my_addr.sin_family = AF_INET; // host byte order
    my_addr.sin_port = htons(MYPORT); // short, network byte order
    my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
    memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
     
    // don't forget your error checking for these calls:
    bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
     
    listen(sockfd, BACKLOG);
     
    sin_size = sizeof(struct sockaddr_in);
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    Last edited by xviddivxoggmp3; 08-12-2005 at 03:31 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    It's kind of a long shot... try to do
    Code:
    sin_size = sizeof(their_addr);
    I know it's the same thing as sizeof(struct sockaddr_in), but you never know.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    how did my previous post not solve the problem?

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    because he followed the guide word by word...
    plus, I also call the function the same way, and it works just fine!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    "works for me" is not very useful. A) Are you using C? If so then of course it works you might try upping your warning level. B) sockets are not very standard so god knows how they are implemented on your machine vs his. It sounds like he is using C++ in which cause his types need to match up, which was covered in my first response.

  11. #11
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i was trying to rewrite it in C++, but was unsuccessful when compiling it with g++.
    i guess my question was not fully asked. I was more interested in the reasoning why it works in C, and not in C++. I know now, but from your prior post, I did not understand that you were saying that unlike C, C++ requires types to match up. So my question for you now is... does the C++ standard and C standard differ in the implimentation of the function calls for sockets? I know from my reading that *nix and windows differ, but on *nix does it also differ between C and C++?
    Last edited by xviddivxoggmp3; 08-12-2005 at 05:50 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Because C is weakly typed and C++ is strongly typed. This means if 2 types don't match, for isntance a socklen_t* and a int*, then your C compiler will, at best, give you a warning. In C++ thiis is an error. And the BSD socket API is written in a sad attempt to provide some sort of inheritence style pointers (that is why things accept pointers to one type but you give it a pointer to another similar type) so in C++ you need to do a lot of explicit casting. You should be using static_cast for you casts by the way, C-style casts are frowned upon.
    And no the API oes not differe. There is only one socket API on your system so it's the same for both languages.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Where did you guys get the socklen_t* from?
    allow me to point you to the MSDN
    SOCKET accept(
    SOCKET s,
    struct sockaddr* addr,
    int* addrlen
    );
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Remember what I said about socket API's not being standard? Let me point you to the manpages on my machine:

    int
    accept(int s, struct sockaddr *addr, socklen_t *addrlen);
    Any questions now Devil Panther?

  15. #15
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I see your point and I bow before your knowledge
    but never the less, we are talking about VC7 here, which implements the msdn.

    Anyway, as orbitz said, just cast... you never know
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create program that accept modules
    By rluceac in forum C++ Programming
    Replies: 16
    Last Post: 04-11-2009, 03:11 PM
  2. could not accept socket
    By Elkvis in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2008, 07:42 AM
  3. Using a single socket for accept() as well as connect()?
    By nkhambal in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-20-2005, 05:43 AM
  4. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 04:59 PM
  5. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 05:50 PM

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