NStream / Sockets / Need Help

Discussion in 'Mac Programming' started by bengut, Jun 15, 2011.

  1. bengut, Jun 15, 2011
    Last edited: Jun 15, 2011

    macrumors newbie

    Joined:
    Jun 15, 2011
    #1
    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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. chown33, Jun 15, 2011
    Last edited: Jun 15, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #3
    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.

    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
     
  4. thread starter macrumors newbie

    Joined:
    Jun 15, 2011
    #4
    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.
     
  5. jiminaus, Jun 16, 2011
    Last edited: Jun 16, 2011

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    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/mac/#documentation/Networking/Conceptual/CFNetwork
     
  6. thread starter macrumors newbie

    Joined:
    Jun 15, 2011
    #6
    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 ?
     
  7. jiminaus, Jun 16, 2011
    Last edited: Jun 16, 2011

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    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.
     
  8. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #8
    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.
     
  9. thread starter macrumors newbie

    Joined:
    Jun 15, 2011
    #9
    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.
     
  10. bengut, Jun 16, 2011
    Last edited by a moderator: Jun 16, 2011

    thread starter macrumors newbie

    Joined:
    Jun 15, 2011
    #10
    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 …
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    Jun 15, 2011
    #12
    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
     
  13. macrumors 603

    Joined:
    Aug 9, 2009
    #13
    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".


    GCDAsyncSocket doesn't do UDP, AFAIK.
     
  14. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #14
    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.
     

Share This Page