C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 06-24-2004, 02:02 PM   #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!
Kristian_ is offline   Reply With Quote
Old 06-24-2004, 04:44 PM   #2
End Of Line
 
Hammer's Avatar
 
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   Reply With Quote
Old 07-01-2004, 01:40 PM   #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   Reply With Quote
Old 07-02-2004, 03:02 PM   #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.
Devil Panther is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22