View Full Version : creating TCP socket
Mar 16, 2008, 12:35 PM
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?
Mar 16, 2008, 12:54 PM
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 ;)
Mar 19, 2008, 09:28 AM
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?
Mar 19, 2008, 12:00 PM
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.
Mar 19, 2008, 06:21 PM
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.
Mar 24, 2008, 07:56 PM
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)
Mar 24, 2008, 08:55 PM
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.
Mar 24, 2008, 09:01 PM
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.
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.