Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 02-16-2009, 12:16 AM   #1
LQ Newbie
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
Typecast struct sockaddr in struct sockaddr_in

Hello Everybody,
I have a very interesting problem while making a simple client server program in c. i am wokring on linux kernel- . While creating a socket for my server program i want to bind it with a port. for which i used following code

struct sockaddr_in sad;
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */
sad.sin_port = htons((u_short)port);/* set the port number */

if (bind(welcomeSocket, (struct sockaddr *)&sad, sizeof(sad)) < 0)
fprintf(stderr,"bind failed\n");
return 1;

Now in this bind call sad is converted in struct sockaddr *. Now after this code what i want to do for some experiment

struct sockaddr *a;
struct sockaddr_in *b;
a=(struct sockaddr *)&sad;
b=(struct sockaddr_in *)a;

now i want to extract all the members of structure b .. but i am getting incompatible reference pointer error. I just want to typecats a struct sockaddr into sockaddr_in and extract all of its data members. Please help me.

Thank you in advance...
Old 02-17-2009, 10:33 AM   #2
LQ Newbie
Registered: Apr 2008
Location: Hampshire, UK
Distribution: None
Posts: 27

Rep: Reputation: 16
You could extract them like this:

((struct sockaddr_in *)&sad)->sin_family
((struct sockaddr_in *)&sad)->sin_port
((struct sockaddr_in *)&sad)->sin_addr

Cheers, Steve


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
about struct sockaddr_in greghua Programming 11 07-29-2005 11:01 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
sockaddr_in and sockaddr AquamaN Programming 4 05-02-2004 03:52 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration