bind and connect function - TCP-IP

This is a discussion on bind and connect function - TCP-IP ; hi, i am now learing linux Socket programn . when i read some codes,that the function bind and connect puzzled me. That is : int bind(int socket,const struct sockaddr *address,size_t address_len) my confusion focus on the sencond parameter,"const struct sockaddr ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: bind and connect function

  1. bind and connect function

    hi, i am now learing linux Socket programn . when i read some
    codes,that the function bind and connect puzzled me. That is :
    int bind(int socket,const struct sockaddr *address,size_t
    address_len)

    my confusion focus on the sencond parameter,"const struct sockaddr
    *address". what does it mean?
    I found it in a programn that is used as "bind(server_socked,(struct
    sockaddr *)&server_address,server_len)".
    what happen to (struct sockaddr *)&server_address?
    As far as i know,&& in C means "AND" or" *",what about "&"?
    My misunderstanding on function connect,i think,is the same as the
    function bind.

    yours sincerly

  2. Re: bind and connect function

    Erfan wrote:

    > hi, i am now learing linux Socket programn . when i read some
    > codes,that the function bind and connect puzzled me. That is :
    > int bind(int socket,const struct sockaddr *address,size_t
    > address_len)
    >
    > my confusion focus on the sencond parameter,"const struct sockaddr
    > *address". what does it mean?


    It means that the parameter must be a pointer to a "struct sockaddr".

    > I found it in a programn that is used as "bind(server_socked,(struct
    > sockaddr *)&server_address,server_len)".
    > what happen to (struct sockaddr *)&server_address?
    > As far as i know,&& in C means "AND" or" *",what about "&"?


    If you have a variable named "var", then "&var" returns the address
    of "var". So, "&server_address" means "the address of variable
    server_address". This address can be used anywhere a pointer to a
    variable of that type is expected.

    The variable "server_address" is not usually of type "struct sockaddr"
    (but instead, for example, "struct sockaddr_in"), so the address obtained
    with "&server_address" is the address (following the example) of a "struct
    sockaddr_in". bind(), OTOH, expects as second parameter the address of a
    plain "struct sockaddr", so the programmer must perform a cast to convert
    the value into the expected type. That's what the (struct sockaddr *) does.

    > My misunderstanding on function connect,i think,is the same as the
    > function bind.


    I suggest that you investigate C pointers and casts.


  3. Re: bind and connect function

    On Mon, 11 Feb 2008 06:29:59 -0800, Erfan wrote:

    > hi, i am now learing linux Socket programn . when i read some
    > codes,that the function bind and connect puzzled me. That is :
    > int bind(int socket,const struct sockaddr *address,size_t
    > address_len)
    >
    > my confusion focus on the sencond parameter,"const struct sockaddr
    > *address". what does it mean?
    > I found it in a programn that is used as "bind(server_socked,(struct
    > sockaddr *)&server_address,server_len)".
    > what happen to (struct sockaddr *)&server_address?
    > As far as i know,&& in C means "AND" or" *",what about "&"?
    > My misunderstanding on function connect,i think,is the same as the
    > function bind.


    This is actually a C question. &server takes the address of the variable
    server. (struct sockaddr *) casts it to the appropriate type for this
    function. If this is chinese to you, I suggest you try in a C oriented
    group.

    HTH,
    M4

+ Reply to Thread