![]() |
| | #1 |
| Registered User Join Date: Jun 2004
Posts: 4
| 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)); Code: connect(fd, "66.102.11.99", sizeof(struct sockaddr)); Code: socklen_t sin_size; Code: sin_size=sizeof(struct sockaddr_in); Thank you! |
| Kristian_ is offline | |
| | #2 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| >>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] |
| Hammer is offline | |
| | #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. |
| dbtid is offline | |
| | #4 |
| * Death to Visual Basic * 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. |
| Devil Panther is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Menu Questions | Olidivera | Windows Programming | 4 | 06-03-2006 05:29 PM |
| Couple of simple directdraw questions. | Deo | Game Programming | 3 | 05-25-2005 07:55 AM |
| A few simple batch questions | sean | A Brief History of Cprogramming.com | 4 | 07-02-2003 01:35 PM |
| Simple Socket Question | SourceCode | Linux Programming | 2 | 06-22-2003 09:20 PM |
| simple socket problem. | threahdead | C Programming | 2 | 01-14-2003 06:20 PM |