Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By alf55
Hi all I am new in c programming and I'm trying to learn something about sockets. I found a line in server side like this in various examples bzero ((char ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! ezequielandrush's Avatar
    Join Date
    Jul 2013
    Posts
    2

    sockets newbie question


    Hi all

    I am new in c programming and I'm trying to learn something about sockets. I found a line in server side like this in various examples

    bzero ((char *) &servaddr, sizeof(servaddr));

    where struct sockaddr_in servaddr; has been defined previously



    Can anyone explain that to me? What exactly does this do?




    Thanks in advance and apologize my english.

    Greetings

    Zeke

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    649
    It is filling sizeof(servaddr) bytes wit zero value starting at the address servaddr ( I.E. it is zero filling the servaddr variable).

    Since the first argument to the function, bzero, need to be a character pointer the "(char*)" casts the following pointer to a "char *" (I.E. a pointer to character). The "&" infront of the variable servaddr gets the address of the variable (which would be "struct sockaddr_in *"). The sizeof returns the number of bytes that the variable is allocated which the second argument of the function, bzero, needs.

    See:
    Code:
    man 3 bzero

  3. #3
    Just Joined! ezequielandrush's Avatar
    Join Date
    Jul 2013
    Posts
    2
    Oh...

    It's just initializing (or erasing) a memory space... Now I feel like an idiot for asking that.

    I guess I need to learn much more than i tought.

    Thanks alf55.

    Good bye

    Zeke

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    . Now I feel like an idiot for asking that.
    No problem - It happens to everyone at times.

    btw,If you haven't came across beej writings Check this out Beej's Guide to Network Programming - I really love beej's writing -
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    649
    Quote Originally Posted by ezequielandrush View Post
    Oh...

    It's just initializing (or erasing) a memory space... Now I feel like an idiot for asking that.

    I guess I need to learn much more than i tought.

    Thanks alf55.

    Good bye

    Zeke
    We learn more by asking questions. We all (even with 33 years of experience) ask questions that when given an answer we think "what an idiot". Enjoy learning.
    Lakshmipathi likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •