Unix Programming - How to initialise the sa_data member in the sockaddr structure

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > How to initialise the sa_data member in the sockaddr structure





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to initialise the sa_data member in the sockaddr structure
jeniffer

2006-04-27, 7:56 am

I need to use the sockaddr structure and want to initialise its sa_data
member.
I know that sa_data contains an address and port number .Plz give me a
simple program to illustrate its use.

struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};

Maxim Yegorushkin

2006-04-27, 7:56 am


jeniffer wrote:
> I need to use the sockaddr structure and want to initialise its sa_data
> member.
> I know that sa_data contains an address and port number .Plz give me a
> simple program to illustrate its use.
>
> struct sockaddr {
> unsigned short sa_family; // address family, AF_xxx
> char sa_data[14]; // 14 bytes of protocol address
> };


You need to use a protocol specific address structure. For ip4 it is
sockaddr_in. sockaddr is a generic data structure used like a base
class, so you need to explicitly cast a sockaddr_in* to sockaddr*.

sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(the_port_number);
bind(sock, (sockaddr*)&sa, sizeof sa);

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com