creating TCP socket

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

  1. macrumors newbie

    Joined:
    Mar 16, 2008
    #1
    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

    Joined:
    Jun 16, 2004
    #2
    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:
    http://cocoadevcentral.com/articles/000061.php

    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

    Joined:
    Mar 16, 2008
    #3
    char * to NSString

    Ok,
    I have come up with something useful, but I still have a problem. For reading from the socket, I use:
    Code:
    while((n=read(connectfd,buffer,200))>0){
    			buffer[n]=0; 
    			printf(buffer);
    		}
    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:
    Code:
    NSMutableString *test = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
    How can I do this properly?
     
  4. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #4
    An easy way is to check the buffer before you do the conversion.

    if (buffer[n-1] == '\n')
    buffer[n-1] = 0;
    else
    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

    Joined:
    Jun 16, 2004
    #5
    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

    Joined:
    Mar 16, 2008
    #6
    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

    Joined:
    Apr 22, 2005
    #7
    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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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.

    -Lee
     

Share This Page