Thread: Simple Socket Questions

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    4

    Unhappy Simple Socket Questions

    Hello friends!
    I'm learning socket programming on Linux. I have some questions what I would like to ask. I hope you will help me!
    That's why, that I'm new to Network Programming, I like to keep things simple.
    When I use connect();, I need to connect like this:
    Code:
    connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr));
    Why can't I use connect like this?
    Code:
    connect(fd, "66.102.11.99", sizeof(struct sockaddr));
    Why can't I just type IP address there, instead of (struct sockaddr *)&server. And what is that sizeof(struct sockaddr) thing? Why do I need it? Now for the server part. When making server, I need to declare this:
    Code:
    socklen_t sin_size;
    And do this:
    Code:
    sin_size=sizeof(struct sockaddr_in);
    And in the end, after socket(); bind(); listen(); I need to accept(); Why does accept need this sin_size as an argument? Is it posible to make Networking app's, without using structures(sockaddr_in or others)?
    Thank you!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>struct sockaddr *)&server
    This is a pointer to a structure that is recognised and used by the connect() function. You can't just put in a string constant, like "127.0.01", it ain't going to work.

    >>sizeof(struct sockaddr)
    sizeof gets you the size of the structure sockaddr in bytes.
    You need it because connect needs to know it.
    Similar answer to the accept() question you've asked, those functions need to know the size of the structure you're passing to them.

    If you want to see sample implementations of these functions, google your way to beej's networking programming pages.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    a) A struct sockaddr contains more than just an IP address.
    It contains information about the destination address (which might
    not even be an IP address!), the port, and some other things.

    Note that it's a (struct sockaddr) not a (struct sockaddr_in).
    The former is a socket address (generic) the second is an
    internet protocol socket address.

    b) accept is going to fill in the number of bytes that the kernel
    dropped into the cliaddr pointer you passed as the second argument
    to it. When you call accept() it uses *addrlen to figure out how big
    the cliaddr is, and when it returns, it fills in the number of bytes stored.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    but why addrlen has to be a pointer?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Menu Questions
    By Olidivera in forum Windows Programming
    Replies: 4
    Last Post: 06-03-2006, 05:29 PM
  2. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  3. A few simple batch questions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-02-2003, 01:35 PM
  4. Simple Socket Question
    By SourceCode in forum Linux Programming
    Replies: 2
    Last Post: 06-22-2003, 09:20 PM
  5. simple socket problem.
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 01-14-2003, 06:20 PM