Re: [Dev-C++] Socket Programming - Newbie - sockaddr 16 bytes?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Obi <ob...@ch...> - 2004-11-06 18:50:12
|
At 02:20 07/11/2004 +0900, Peter Dennis wrote:
>Hi,
>
>Can someone tell me why it is important to fill out a sockaddr structure
>to 16 bytes.
>
>Reading a book it says that the structure is as follows:
>
>struct sockaddr {
> unsigned short sa_family;
> char sa_data[14];
>}
>
>Why fill it out to 16 bytes? Is this a magic number in computers?
>
>If the address family I choose is AF_INET then how do I know that this
>takes only 2 bytes in the structure? Is there a way to calculate this?
>How do I know that I need to set sa_data to 14 and not 15?
>
>Any information regarding this would be great.
>
>Many thanks,
>Peter.
socakaddr is a generic structure for all family types used to pass addresses
to functions that don't depend on the family type.
If you use AF_INET you should use the sockaddr_in definition to manipulate
the data.
Use a reinterpret_cast<> to swap between the two defs for the
same in-memory data.
e.g.
m_listenSocket = socket( AF_INET, SOCK_STREAM, 0 );
// Bind Socket
sockaddr_in lsa;
memset( &lsa, 0, sizeof(lsa) );
lsa.sin_family = AF_INET;
lsa.sin_port = htons( LISTEN_PORT );
lsa.sin_addr.s_addr = htonl( INADDR_ANY );
SOCKET ls;
bind( ls, reinterpret_cast<sockaddr*>( &lsa ), sizeof(lsa) );
// Listen on socket
listen( ls, QSIZE );
Hope this helps,
O
|