Simple networking?

Moderator
Posts: 102
Joined: 2003.07
Post: #1
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 Smile

-CarbonX
Quote this message in a reply
Member
Posts: 70
Joined: 2004.06
Post: #2
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.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #3
Although it's not in Cocoa, you may also want to use the CFNetwork stuff().

Wade
Quote this message in a reply
Moderator
Posts: 102
Joined: 2003.07
Post: #4
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

-CarbonX
Quote this message in a reply
anonuser
Unregistered
 
Post: #5
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.
Quote this message in a reply
Moderator
Posts: 102
Joined: 2003.07
Post: #6
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
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #7
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
Quote this message in a reply
Moderator
Posts: 102
Joined: 2003.07
Post: #8
ah, must have missed that... thanks
Sneaky

-CarbonX
Quote this message in a reply
Moderator
Posts: 102
Joined: 2003.07
Post: #9
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
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.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #11
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
Not only the external IP, but the external port, too. Port numbers may be changed by travel through a router.
Quote this message in a reply
Moderator
Posts: 102
Joined: 2003.07
Post: #13
ok, thanks for clarifying that guys Smile

-CarbonX
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #14
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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Various BSD Networking Questions wyrmmage 25 10,538 Jul 30, 2007 05:09 AM
Last Post: unknown
  Newbie networking - just dl'ing data from an URL... sealfin 4 3,418 Apr 19, 2007 07:01 AM
Last Post: sealfin
  Good networking library Ummon 5 4,389 Jan 22, 2007 11:59 PM
Last Post: akb825
  Saving a Place for Networking Roosterhouse 3 3,901 Aug 6, 2004 02:05 PM
Last Post: FCCovett
  Networking for Multiplayer Games Where to start? NYGhost 2 3,385 Jan 28, 2004 09:15 AM
Last Post: Skorche