Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pieterdhondt

macrumors newbie
Original poster
Mar 16, 2008
4
0
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?
 

sord

macrumors 6502
Jun 16, 2004
352
0
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 ;)
 

pieterdhondt

macrumors newbie
Original poster
Mar 16, 2008
4
0
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?
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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.
 

sord

macrumors 6502
Jun 16, 2004
352
0
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.
 

pieterdhondt

macrumors newbie
Original poster
Mar 16, 2008
4
0
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)
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.