Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 15, 2011, 09:12 AM   #1
bengut
macrumors newbie
 
Join Date: Jun 2011
NStream / Sockets / Need Help

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 by bengut; Jun 15, 2011 at 03:55 PM. Reason: Referencing Cocoa Class NSStream
bengut is offline   0 Reply With Quote
Old Jun 15, 2011, 10:46 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Jun 16, 2011, 01:13 AM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
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.

Quote:
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/m...ion/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_co...ion_networking
http://en.wikipedia.org/wiki/IP_multicast

Last edited by chown33; Jun 16, 2011 at 01:23 AM.
chown33 is offline   0 Reply With Quote
Old Jun 16, 2011, 03:05 AM   #4
bengut
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by chown33 View Post
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/m...ion/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_co...ion_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.
bengut is offline   0 Reply With Quote
Old Jun 16, 2011, 03:45 AM   #5
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by bengut View Post
How do I implement "listening" on a "socket" with NSStream ?
You can't. NSStream is only for client-side communication, or for communication once a socket has already been connected to.

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

Otherwise, look at the Core Foundation Networking layer. http://developer.apple.com/library/m...tual/CFNetwork

Last edited by jiminaus; Jun 16, 2011 at 04:01 AM.
jiminaus is offline   0 Reply With Quote
Old Jun 16, 2011, 04:41 AM   #6
bengut
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by jiminaus View Post
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 ?

Quote:
Originally Posted by jiminaus View Post
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 ?
bengut is offline   0 Reply With Quote
Old Jun 16, 2011, 06:27 AM   #7
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by bengut View Post
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 by jiminaus; Jun 16, 2011 at 07:01 AM.
jiminaus is offline   0 Reply With Quote
Old Jun 16, 2011, 07:16 AM   #8
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: 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.
xStep is offline   0 Reply With Quote
Old Jun 16, 2011, 08:06 AM   #9
bengut
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by xStep View Post
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 is offline   0 Reply With Quote
Old Jun 16, 2011, 08:09 AM   #10
bengut
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by jiminaus View Post
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 kainjow; Jun 16, 2011 at 11:13 AM. Reason: fixed quote
bengut is offline   0 Reply With Quote
Old Jun 16, 2011, 12:35 PM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by bengut View Post
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.
chown33 is offline   0 Reply With Quote
Old Jun 16, 2011, 01:36 PM   #12
bengut
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
Quote:
Originally Posted by chown33 View Post
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
bengut is offline   0 Reply With Quote
Old Jun 16, 2011, 02:18 PM   #13
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by bengut View Post
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/25058...r-old-projects

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


Quote:
The Class is called GCDAsyncSocket know. But this class is even more sophisticated
GCDAsyncSocket doesn't do UDP, AFAIK.
chown33 is offline   0 Reply With Quote
Old Jun 16, 2011, 05:59 PM   #14
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Less lost in L.A.
Quote:
Originally Posted by bengut View Post
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.
Bonjour is also available for MS Windows from Apple. I don't know about other OS's though I think there has been some work outside of Apple on it.
xStep is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
nMP - question about TB2 sockets reliability 2128506 Mac Pro 49 Jan 19, 2014 10:53 PM
mbpro charger plug for alternate sockets zoran Mac Peripherals 3 Sep 1, 2013 12:31 PM
Divert sockets blazerguns Mac Programming 2 Feb 16, 2013 02:31 PM

Forum Jump

All times are GMT -5. The time now is 01:50 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC