creating TCP socket

Discussion in 'Mac Programming' started by pieterdhondt, Mar 16, 2008.

  1. macrumors newbie

    Mar 16, 2008
    I have to write a relatively simple application in Cocoa which involves setting up a TCP socket which listens on port 6789, creates a new incoming connection, reads one string and then closes the socket. That's really all it has to do, but it's apparently quite hard... I'm new to the whole mac programming and networking appears to be tough. Can someone help me by supplying some sample code on how to achieve this?
  2. macrumors 6502

    Jun 16, 2004
    There are some NS classes for use with sockets I believe, but I usually just stick with Unix sockets. You will need another thread so 1 thread can drive the UI, and the second can drive your socket.

    Creating the thread is actually pretty simple, take a look here:

    From there, just drop in your socket code into the new thread, and if your UI thread and socket thread need to share data make sure you toss a mutex around the global variable.

    A quick Google for 'bsd sockets tutorial' (no quotes) comes up with a lot of references.

    If you want to use the NSSocketPort, a Google for 'cocoa sockets tutorial' comes up with some as well.

    If you start putting code together and get stuck, just post again ;)
  3. thread starter macrumors newbie

    Mar 16, 2008
    char * to NSString

    I have come up with something useful, but I still have a problem. For reading from the socket, I use:
    Then with the received text (which is a url), I want to convert to a NSString, but when I do that, there is alwas %0A at the end... I do it this way:
    NSMutableString *test = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
    How can I do this properly?
  4. macrumors 68020


    Apr 12, 2006
    Long Island
    An easy way is to check the buffer before you do the conversion.

    if (buffer[n-1] == '\n')
    buffer[n-1] = 0;
    buffer[n] = 0;

    I'm not up to speed on Obj C, but there might be an rtrim function of some sort where you can replace the character after the conversion.
  5. macrumors 6502

    Jun 16, 2004
    Not sure about the %0A issue without seeing the client/server, however one thing to keep in mind, if your buffer gets written to twice from the while loop, your code will not execute as expected.
  6. thread starter macrumors newbie

    Mar 16, 2008
    working in background

    Ok, my application does what it has to do, but I just have one more issue.
    When I receive the url, I open up Safari with the given url, no problems with that. But then, when I send another url to my application, the socket seems to be closed. So, it seems like it shuts down my app or something... (it's an iPhone app by the way)
  7. macrumors 68040

    Apr 22, 2005
    Your application quits when Safari opens up. Theoretically there might be a way to run it in the background, but officially Apple will not allow 3rd party background applications.
  8. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Note: I have not read iPhone dev docs. i don't know what the best practices are.

    I would treat the iPhone as the client for everything. I would not make it accept connections. I'm sure there's a niche reason to do so in some cases, but it seems like it would be MUCH better to connect to a remote server on a pre-defined port and have it push some XML or the raw address if that's truly all you need.

    Once you've gotten the 1 url over, it seems like you can handle subsequent ones through the browser. Have the page prompt to take the user to a new page w/ javascript, etc.

    Maybe you can give us some more specifics and we can point you in the right direction. As admanimal stated, background tasks are verboten. Even if you can make your app do it through trickery, I *know* that violates the recommended guidelines apple has set out.


Share This Page