c++ socket programming,chat server

my first post,
hi every one,i just start to learn socket programming with c++,i'm new,
so i start with a chat server and client,
A:
my first question is about the memset,
 
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct 

i know sin_zero must be filled with zero,what i wanna know is,
does the & here indicate to the memory address where sin_zero will be,so it will fill it with zero?!!

B:
 
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))

i just can't figure this out,would you please give some more detail about this part:
 
,(struct sockaddr *)&my_addr,

thanks in advance
Last edited on
One question- did you actually learn C++ before you started to learn socket programming? Because I'd suggest you to do that first.

Also, do you have programming experience in general? Cause a chat client is definitely not simple for a beginner.
i know c++ a little bit and still learning,
 
Cause a chat client is definitely not simple for a beginner.

well that was the first example i could find whenever i found a tutorial.
so would you please answer my question,thanks,
For question A

Yes, You are passing the address fo my_addr.sin_zero

For question B

I will give you a hint, so that you can read about it and understand.

it is something related to Type Casting
I recommend the Stevens book, UNIX Network Programming, for starters.

The socket APIs in Un*x have to deal with different types of sockets. For example,
there are UDP sockets and UNIX sockets. When you bind an address to a socket,
you are essentially telling the operating system that your socket should receive
all data that is sent to the address that you are binding. For the networking world,
addresses are comprised of an IP address and a port number. But, for the UNIX
socket world, addresses are actually strings instead.

The varied nature of the address format makes for some difficulty in the C world,
since I have to pass completely different types to bind() (and other system calls)
for the address.

C's solution to this is ersatz polymorphism. For internet-based sockets, there
is struct sockaddr_in, which contains members for the IP address and port.
For UNIX-based sockets, there is struct sockaddr_un, which contains a member
for the name (string). Since these are two completely different types, you have
to do two things: first, you have to typecast your struct instance to a common
type (struct sockaddr) to make the compiler happy. Second, you have to fill out
the "family" member of your address struct to tell the kernel what kind of address
you are passing -- a struct sockaddr_in, a struct sockaddr_un, or something else.

struct sockaddr is merely a facade; it contains, as its first member, the "family"
member (which you may know as sin_family), and the rest of the struct is
essentially padding, so that sizeof( sockaddr ) is at least the size of the largest
address struct you might pass in.

That said, your code in B is slightly flawed in that you should pass
 
sizeof( my_addr )
as the third parameter.

thanks for helping jsmith,
and srinathduraisamy i'm trying to learn more about typecasting,i will let you know if i face with problem
i really appreciate for helping,
Let's look at:

(struct sockaddr *)&my_addr


in detail. When decoding C types, you always start with the variable, my_addr in this case, and go left-right, left-right. In this case there is nothing to the right of my_addr so the task is simplified. So, the next element is:

&my_addr


Every elementary C programmer knows this is "address of" my_addr. The next element is a typecast, because it's on the left and in parenthesis. The type we are casting to is:

struct sockaddr *


So the type is, moving right-to-left, "pointer to" "struct sockaddr". So what we have is the address of my_addr, typecast to pointer to struct sockaddr. Easy enough when you learn to read the type this way, right?

By the way, jsmith was wrong on the third parameter. Since you've cast the argument to bind to a sockaddr, you need to pass in the length of a sockaddr, not whatever random length my_addr might have had. In actual practice, most likely either will work.
thank you softweyr,
your answer is really helpful,and yet another question,(sorry for being totally noob)
so we type cast &my_addr (which is the address of my_addr) to struct sockaddr_in,
so all we have is a pointer to struct sock_addr in,
if i am right would you please tell me what is the use to type cast?
i mean why we type cast?
Topic archived. No new replies allowed.