Thread: the socket

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    the socket

    hello, I want to develop a program that interact with the browser using the socket but I can't understand the difference between the two structure
    Code:
    struct sockaddr {  }
    and
    Code:
    struct sockaddr_in {   }
    and why casting sockaddr_in to sockaddr

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    "struct sockaddr" is a generic address, while "struct sockaddr_in" is an IPv4 address.
    and why casting sockaddr_in to sockaddr
    It allows functions like accept(2) to work for any address type (that is, you don't need accept_ipv4(), accept_ipv6(), and so on). All of the struct sockaddr types will have a common beginning sequence which contains the address family, so accept(2) can simply peek at that to decide how to proceed.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It's a nice example of class inheritance in C. In OOP terms, sockaddr is the base class, and sockaddr_in and sockaddr_in6 (and others like sockaddr_un) are derived classes.

    Here's a good page that explains what these structures are in more depth: struct sockaddr and pals

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VC++ socket
    By valthyx in forum Windows Programming
    Replies: 9
    Last Post: 08-01-2011, 08:58 AM
  2. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  3. n00b doing a Socket operation on non-socket
    By Kinasz in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-25-2004, 03:29 AM
  4. TCP/IP Socket
    By Nor in forum Linux Programming
    Replies: 4
    Last Post: 04-22-2002, 11:21 AM
  5. socket to me
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2002, 05:32 AM