Q. Socket function arguments!

This is a discussion on Q. Socket function arguments! within the Networking/Device Communication forums, part of the General Programming Boards category; Hello! I know how to use connect, bind, accept, but I have no idea how they work! For example connect(); ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Q. Socket function arguments!

    Hello!
    I know how to use connect, bind, accept, but I have no idea how they work!
    For example connect(); takes three arguments:

    int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

    For example this is how it would look in the code:
    connect(sock_fd, (struct sockaddr *)&server, sizeof(struct sockaddr))

    I understand the first argument! The second argument is memory address of the sockaddr structure? or is it memory address of the object server? And if its memory address of the object server, why can't I just write &server, instead of(struct sockaddr *)&server? And does * mean that it has to do something with pointers?

    The last argument is size of the sockaddr structure! But why sockaddr structure? I use sockaddr_in NOT sockaddr structure!!

    And when I do this:
    cout << sizeof(struct sockaddr_in) << endl;
    and this:
    cout << sizeof(struct sockaddr) << endl;

    Both structure size is 16! What does 16 mean? Size of all variables in the structures or what?

    And last question! What do all thous headers mean?
    For example <sys/socket.h> contains socket(); connect(); etc...
    But what about <sys/types.h>; <netinet/in.h> and <arpa/inet.h>? Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Read this.
    Useful Links And Good Books

    > Both structure size is 16! What does 16 mean? Size of all variables in the structures or what?
    If you really don't know, then go back to learning C++ and come back to networking when you understand what it happening at a basic level.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Well, as far as I know it returns the size of an array! Am I right? Thank you!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > Am I right?
    No

    > Thank you!
    You're welcome
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    OK, can you at least tell me what is (struct sockaddr *)&server?
    Why does this line has both * and &? * is for pointer and & is when you create reference, but why are they both here?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    http://www.ecst.csuchico.edu/~beej/guide/net/html/

    Read all of that really throughly and you will understand most of your questions, and allot of more! If you don't get it at once, read it again and again, finally it comes clear!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Thank you!

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    OK, this is the answer that I found:
    The second argument is a pointer to a struct sockaddr, that contains information about your addresses, namely, port and IP address.
    But what is this?
    (struct sockaddr *)&server
    is it a reference or what?
    For example you define pointer like this:
    int *pointer=0;
    and reference like this:
    int &reference;
    So (struct sockaddr *)&server a pointer to a struct sockaddr. But what does &server do? Variables will return memory address with &! So what is it? variable, reference or something else?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    (struct sockaddr *) - this is a cast - in particular it's a cast to a pointer to a struct sockaddr
    & - this is the address of operator
    server - this is a variable - presumably not a struct sockaddr otherwise the cast would be unnecessary

    So the whole thing is a pointer to a server, and that pointer cast to a pointer to a struct sockaddr
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 12:38 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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