Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
Hi

I am trying to establish a simple communication between two Mac's using the NSStream Class (Cocoa).

Establishing a socket, sending some minor data via outputStream, receiving some minor data via InputStream.

There is no true server / client relation in this communication. I.e. there is never a request from one to the other. They simply send it's Data triggered by a users key hit event. And the otherone receives the data without answering. Also the data is always the same size. Exact 11 bytes …

Is there anyone out there who may have a suggestion how to code this ?

thanks
bengut
 
Last edited:

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Off the top of my head, probably most flexible way to do this is through Bonjour with NSNetService and NSNetServiceBrowser. That lets each system auto-discover each other and then open up a connection to the other and send/receive data.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
There is no true server / client relation in this communication. I.e. there is never a request from one to the other.
Of course there is. The sender sends a request, and the listener listens. If no one is listening, then nothing happens. Both sides can both do both things at once, but if there isn't a listener, then no network protocol is going to keep the sender's message warmed-up and waiting for you. You snooze you lose.

They simply send it's Data triggered by a users key hit event. And the other one receives the data without answering. Also the data is always the same size. Exact 11 bytes …
UDP datagrams.
http://en.wikipedia.org/wiki/User_Datagram_Protocol


Get the CocoaEcho sample code:
http://developer.apple.com/library/mac/#samplecode/CocoaEcho/Introduction/Intro.html

It's not UDP, but it shows what you can do with Bonjour (which are UDP multicast datagrams).

http://en.wikipedia.org/wiki/Bonjour_(software)
http://en.wikipedia.org/wiki/Zero_configuration_networking
http://en.wikipedia.org/wiki/IP_multicast
 
Last edited:

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
Of course there is. The sender sends a request, and the listener listens. If no one is listening, then nothing happens. Both sides can both do both things at once, but if there isn't a listener, then no network protocol is going to keep the sender's message warmed-up and waiting for you. You snooze you lose.

You are right … but this happens on TCP Level ore below. In the context of myApp there is no request, so all the examples with client/server does not really match my needs. Question : How do I implement "listening" on a "socket" with NSStream ?


UDP datagrams.
http://en.wikipedia.org/wiki/User_Datagram_Protocol


Get the CocoaEcho sample code:
http://developer.apple.com/library/mac/#samplecode/CocoaEcho/Introduction/Intro.html

It's not UDP, but it shows what you can do with Bonjour (which are UDP multicast datagrams).

http://en.wikipedia.org/wiki/Bonjour_(software)
http://en.wikipedia.org/wiki/Zero_configuration_networking
http://en.wikipedia.org/wiki/IP_multicast

---------------------------------------------------
There are only 10 kinds of people in the world:
Those who understand binary, and those who don't.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Last edited:

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
You can't. NSStream is only for client-side communication, or for communication once a socket has already been connected to.

That's the point: NSStream is for "client-side communication". It can't work if there is no server up on the other side, as in my case. So I think the term server/client does not match for my application.

But which Class to use then ?

I think you can do this much more simply by directly using the BSD sockets API.

Yes , for shure. But BSD Sockets API is the lowest level and you have to take care about a lot of yourself... puhhh.
Do you know someone who is familiar with this level ?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
That's the point: NSStream is for "client-side communication". It can't work if there is no server up on the other side, as in my case. So I think the term server/client does not match for my application.

But which Class to use then ?

Yes , for shure. But BSD Sockets API is the lowest level and you have to take care about a lot of yourself... puhhh.
Do you know someone who is familiar with this level ?

There isn't a class.

Your dealing with a peer-to-peer situation. They're both servers and clients to each other. Each will need to have sockets on which they're listening, and each will have to send to each other's listening sockets.

There is nothing at the Cocoa level for this. CFNetwork is as high as you can go. At least amongst the standard libraries.

EDIT: Actually if you have of actually looked at the CocoaEcho sample chown33 pointed out to you, you would have found that Apple has provided a TCPServer class that you could reuse.
 
Last edited:

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
I'm not sure what I'm missing in this conversation. Here are some thoughts from my rusty experience using Bonjour with a P2P game I was working on for iOS. Also from what I recall, setting up sockets in OS X can be easier than iOS because a few extra methods are available on OS X.

There are two parts to this problem. The actual socket connection to exchange data, and a service to connect to. Bonjour allows for easy advertising of the service and connection. It can save you from worrying about IP addressing and precise port numbers.

Using Bonjour you can have the app on each box advertise it self. If both boxes have the service advertised, they can then find each other and you can have one make a connection to other and send data to it, and vise-versa. It isn't required to be a two way conversation, in that once box A has found box B, box A can setup the connection to box B and start sending data.

NSStream isn't restricted to client only. It does however as jiminaus mentioned, require a socket. In a P2P setup, both devices are both a client and a server.

Search for 'Bonjour Overview', no quotes, in your Xcode developer documentation or on the Apple developer site. Apple also has some sample code. WiTap might be the sample to start at. Another sample to look into is SimpleNetworkStreams. Head the very first sentence in that document.

It was also mentioned to check out CFNetServices.
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
I'm not sure what I'm missing in this conversation. Here are some thoughts from my rusty experience using Bonjour with a P2P game I was working on for iOS. Also from what I recall, setting up sockets in OS X can be easier than iOS because a few extra methods are available on OS X.

There are two parts to this problem. The actual socket connection to exchange data, and a service to connect to. Bonjour allows for easy advertising of the service and connection. It can save you from worrying about IP addressing and precise port numbers.

Using Bonjour you can have the app on each box advertise it self. If both boxes have the service advertised, they can then find each other and you can have one make a connection to other and send data to it, and vise-versa. It isn't required to be a two way conversation, in that once box A has found box B, box A can setup the connection to box B and start sending data.

NSStream isn't restricted to client only. It does however as jiminaus mentioned, require a socket. In a P2P setup, both devices are both a client and a server.

Search for 'Bonjour Overview', no quotes, in your Xcode developer documentation or on the Apple developer site. Apple also has some sample code. WiTap might be the sample to start at. Another sample to look into is SimpleNetworkStreams. Head the very first sentence in that document.

It was also mentioned to check out CFNetServices.

Thanks xStep for your words. I think I described the connection not correctly in detail. I wrote that I want to connect 2 Macs, but in fact I also want to connect a PC. So I think Bonjour and Netservices are not the choice.
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
There isn't a class.

Your dealing with a peer-to-peer situation. They're both servers and clients to each other. Each will need to have sockets on which they're listening, and each will have to send to each other's listening sockets.

There is nothing at the Cocoa level for this. CFNetwork is as high as you can go. At least amongst the standard libraries.

EDIT: Actually if you have of actually looked at the CocoaEcho sample chown33 pointed out to you, you would have found that Apple has provided a TCPServer class that you could reuse.

I had already a look on CocoaEcho, so I did not reply to this. But I think I have to look closer on it again. Thanks …
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
Thanks xStep for your words. I think I described the connection not correctly in detail. I wrote that I want to connect 2 Macs, but in fact I also want to connect a PC. So I think Bonjour and Netservices are not the choice.

Get AsyncUDPSocket:
http://code.google.com/p/cocoaasyncsocket/

You can use it as-is, or read its source to see how to do things yourself.

It probably won't help you writing the PC code.
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
Get AsyncUDPSocket:
http://code.google.com/p/cocoaasyncsocket/

You can use it as-is, or read its source to see how to do things yourself.

It probably won't help you writing the PC code.

Thanks for your tip. I found AsyncSocket and explore it at the moment and I managed to send and receive bytes. But still this examples follows a server and client schema. As jiminaus pointed it out earlier my application is both at the same time. A sever and a client. So finally, I have to trace the code to understand what is doing what and going where. At least it seems to meet the kind of level I need for my purposes.

By the way using AsyncSocket with XCODE 3.x running under OS X 10.6.x is not straightforward. The code example (project) is written in 2003 and while compiling it causes this little error:
------
Jam is deprecated and has been removed; targets that use Jam must be upgraded to native targets. For more information on doing this, consult the Xcode documentation.
------
So maybe there is someone out there who is interested to update this project. Actuallly it is maintainted by deusty.com, but they upgraded it to a higher level with Grand Central Dispatch (instead of using RunLoops). The Class is called GCDAsyncSocket know. But this class is even more sophisticated …

I just wanted to send some fixed amounts of bytes to a connected computer :( nada mas …

Anyway, if somebody is interested to support me with my app I would appreciate very much.

cheers
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
By the way using AsyncSocket with XCODE 3.x running under OS X 10.6.x is not straightforward. The code example (project) is written in 2003 and while compiling it causes this little error:
------
Jam is deprecated and has been removed; targets that use Jam must be upgraded to native targets. For more information on doing this, consult the Xcode documentation.
------

Where did you get a version from 2003?
I don't see the problem at all here. The mod dates on my files are from 2009.


Xcode has a menu item: Upgrade to Native Target
http://blog.natesilva.com/post/250581112/xcode-wont-build-a-universal-binary-for-old-projects

In 3.2.*, its name may be "Upgrade all Targets in Project to Native".


The Class is called GCDAsyncSocket know. But this class is even more sophisticated …
GCDAsyncSocket doesn't do UDP, AFAIK.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.