What is bzero and Why to Use sin_zero for Zero Padding in Socket Server and Client

Spread the love

The question that I have mentioned in the title “What is bzero and Why to use sin zero for zero padding in Socket Server and Client” of the article is related to “Socket Server” in C programming for TCP/IP communication. Those who are from the computer science or engineering background, maybe they used to do this kind of code as their project work. Also software developers who design Socket Server and Socket Cleint, they also know about this topic. But I am not sure how many of them are really clear about the question that I have mentioned in the article title. But this is very important for the beginners to get a clear idea while doing the programming of Socket Server and Socket Client. That is why I am explaining this topic.

To get a clear idea about bzero and sin_zero, we really need to have a clear idea about struct sockaddr_in and struct sockaddr. But before going to inside, the answer of the question is we use bzero to make the size of struct sockaddr_in and struct sockaddr equal. Now the question comes, then what are the structures of this two types of struct. The details is below:

struct sockaddr {
sa_family; //2 bytes
sa_data; // 14 bytes

};

and

struct sockaddr_in{

sin_family; // 2 bytes
sin_port; //2 bytes
sin_addr; // 4 bytes is the IP Address
sin_zero; // 8 bytes in size and provided only to ensure 16 bytes

}

So, from the above details it is clear that we are using sin_zero to put extra 8 bytes of zero to make the size equal to the size of struct sockaddr. As struct sockaddr_in needs to be cast to struct sockaddr, it has to be kept the same size.

Now, the question comes why we need to cast sockaddr_in to sockaddr when calling bind()?

note: bind (sock, (struct sockaddr*)&server, sockaddr_len)

The answer is sockaddr is a descriptor for any kind of socket operation whereas sockaddr_in is a struct specific to IP Based communication. So, while binding we need to cast it with struct sockaddr. I think the above explanation will help you to understand the concept. If you have more question, you can ask me through comments.

Himadri Subrah Saha

Himadri is an ICT Professional who writes for his technology tips & tricks related blog TechnTechie. Though it is hard to balance time in between professional life and blogging, he still manages time to work for his own blog and writes almost regularly. The dashboard of this WordPress is the only place where he does not feel tired! Read my other blogs @ PetCare and Teleinfo

0 0 votes
Article Rating

Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
investasi
8 years ago

Nice post. I learn something new and challenging on blogs I stumbleupon every day.

It’s always exciting to read through articles from other authors and use something from their
sites.

dirsa
7 years ago

This excellent website really has all the information I needed about this subject and didn’t know who to ask.

https://twitter.com/
7 years ago

you are in point of fact a good webmaster. The site loading velocity is
amazing. It kind oof feels that you are doing any distinctive trick.
Also, The contents are masterwork. you’ve done a wonderful task in this subject!

grawer w szkle lubin
6 years ago

Hurrah, that’s what I was exploring for, what a material! present here
at this webpage, thanks admin of this site.

5
0
Would love your thoughts, please comment.x
()
x