Simple networking?
Could somebody please point me in the direction or give me some source on how to write a simple send/recieve networking app that works over the internet (I've looked on google for stuff but it all seems to be rendezvous)
(cocoa code is best but any code that can do this is helpful)
basically what I need is something that you can do a direct IP connect over a specified port or something that shows how to send/receive data (how to retrieve the data from the socket and put it back into (string, int, etc) then print it to the window)
sorry if this sounds a little strange but I'm not really sure how to explain it well.
suggestions and help are much appreciated, thanks in advance
(cocoa code is best but any code that can do this is helpful)
basically what I need is something that you can do a direct IP connect over a specified port or something that shows how to send/receive data (how to retrieve the data from the socket and put it back into (string, int, etc) then print it to the window)
sorry if this sounds a little strange but I'm not really sure how to explain it well.
suggestions and help are much appreciated, thanks in advance
-CarbonX
I actually have made myself a basic socket class, I could give it to you too if you wish. It can connect to a specified server/port, and you can read and write using void *, NSData or NSString. You can also put a timeout on the connect, send and receive. It's written in Cocoa.
Otherwise, there's always Beej's guide. It's very good.
Otherwise, there's always Beej's guide. It's very good.
Although it's not in Cocoa, you may also want to use the CFNetwork stuff().
Wade
Wade
Cool, I think I'll take a look at SOUR-monkey's class, my iChat addy is <carbonx@mac.com>or you can email it to me at <carbonx@mail.xmission.com>
thanks
thanks
-CarbonX
search for BSD Sockets.
they have tons of tutorials.
And its as basic as it gets.
now you need to decided UDP or TCP different protocols different ways of sending.
CFNetwork API, is basically a wrapper. I do not know of a Cocoa counter part.
but look at CFStream.
Apple CFNetwork Page
Beej's Guide to Network Programming - i'd suggest going over this before you dive in to a wrapper to know what is going on.
they have tons of tutorials.
And its as basic as it gets.
now you need to decided UDP or TCP different protocols different ways of sending.
CFNetwork API, is basically a wrapper. I do not know of a Cocoa counter part.
but look at CFStream.
Apple CFNetwork Page
Beej's Guide to Network Programming - i'd suggest going over this before you dive in to a wrapper to know what is going on.
One more quick question... when calling listen() or accept(), do you have to call them in some sort of run loop? otherwise how does accept know that there's a connection pending... this is not described very clearly in beej's tutorial or at least not from what I could see, thanks again
-CarbonX
In the most simple form, you'd call listen, followed by accept. Accept would then block and sit there until a connection was made.
However, that's probably not what you want. The next choice would be to make the socket non-blocking, in which case accept would return immediately, but the only way to know if a connection is pending is to sit there and poll it continuously, eating up CPU cycles. Also probably not what you want.
So, the right way to do it is with select. Select allows you to check the status of sockets for reading and writing. So, you'd call select on your listening socket - if there's data there to read, it means a connection is pending, at which point you'd call accept. If there's no data there to read, you'd skip the accept and go do something else and come back and check later. Beej's tutorial explains all this in the section on select().
Wade
However, that's probably not what you want. The next choice would be to make the socket non-blocking, in which case accept would return immediately, but the only way to know if a connection is pending is to sit there and poll it continuously, eating up CPU cycles. Also probably not what you want.
So, the right way to do it is with select. Select allows you to check the status of sockets for reading and writing. So, you'd call select on your listening socket - if there's data there to read, it means a connection is pending, at which point you'd call accept. If there's no data there to read, you'd skip the accept and go do something else and come back and check later. Beej's tutorial explains all this in the section on select().
Wade
ah, must have missed that... thanks
-CarbonX
hmmm... I can't seem to get the computer's (host's) public IP address to do a direct connection... I tried using [[NSHost currentHost] address] and it seems to report properly on a dialup connection, but on my DSL it reports the address as IPv6... does anybody know any way to get the host's public IP? (or IP as people on the internet would see it)
-CarbonX
You can't do it. It's not possible.
As you've discovered, if you're directly connected to the 'net (dialup, cable) you can probably find your external IP without problems, but if you're behind a router, you can't.
If you can send a message to the other party, they can extract from the network layer the IP address and port that your message appeared to have come from, which is where return traffic should be sent.
Ultimately though, one party needs to be a server, at a known external IP address with a fixed port open, for the other to connect to.
As you've discovered, if you're directly connected to the 'net (dialup, cable) you can probably find your external IP without problems, but if you're behind a router, you can't.
If you can send a message to the other party, they can extract from the network layer the IP address and port that your message appeared to have come from, which is where return traffic should be sent.
Ultimately though, one party needs to be a server, at a known external IP address with a fixed port open, for the other to connect to.
Along the same lines as what Keith is suggesting, you also can use a metaserver. This is a server process that would run on some Internet-accessible machine. All clients contact the metaserver and it tells the clients what the public IP addresses are.
Wade
Wade
Not only the external IP, but the external port, too. Port numbers may be changed by travel through a router.
ok, thanks for clarifying that guys
-CarbonX
check out BlackHole Media's NetSocket. It's a nice objective-C wrapper they wrote around CFSocket: http://www.blackholemedia.com/code/
They include examples of how to use the class, including a full-out chat program (which is similar in many ways to what you're looking to do).
They include examples of how to use the class, including a full-out chat program (which is similar in many ways to what you're looking to do).
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Various BSD Networking Questions | wyrmmage | 25 | 10,179 |
Jul 30, 2007 05:09 AM Last Post: unknown |
|
| Newbie networking - just dl'ing data from an URL... | sealfin | 4 | 3,235 |
Apr 19, 2007 07:01 AM Last Post: sealfin |
|
| Good networking library | Ummon | 5 | 4,181 |
Jan 22, 2007 11:59 PM Last Post: akb825 |
|
| Saving a Place for Networking | Roosterhouse | 3 | 3,726 |
Aug 6, 2004 02:05 PM Last Post: FCCovett |
|
| Networking for Multiplayer Games Where to start? | NYGhost | 2 | 3,234 |
Jan 28, 2004 09:15 AM Last Post: Skorche |
|

