Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 16, 2008, 11:35 AM   #1
pieterdhondt
macrumors newbie
 
Join Date: Mar 2008
creating TCP socket

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?
pieterdhondt is offline   0 Reply With Quote
Old Mar 16, 2008, 11:54 AM   #2
sord
macrumors 6502
 
Join Date: Jun 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:
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
__________________
12" iBook 1GHz 768MB RAM 60GB HD Airport Extreme [sold and missed]
17" iMac G5 1.6GHz 768MB RAM 160GB HD [sold]
PowerMac Quad 2.5GHz G5 2.5GB RAM
sord is offline   0 Reply With Quote
Old Mar 19, 2008, 08:28 AM   #3
pieterdhondt
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
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?
pieterdhondt is offline   0 Reply With Quote
Old Mar 19, 2008, 11:00 AM   #4
pilotError
macrumors 68020
 
pilotError's Avatar
 
Join Date: Apr 2006
Location: 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.
pilotError is offline   0 Reply With Quote
Old Mar 19, 2008, 05:21 PM   #5
sord
macrumors 6502
 
Join Date: Jun 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.
__________________
12" iBook 1GHz 768MB RAM 60GB HD Airport Extreme [sold and missed]
17" iMac G5 1.6GHz 768MB RAM 160GB HD [sold]
PowerMac Quad 2.5GHz G5 2.5GB RAM
sord is offline   0 Reply With Quote
Old Mar 24, 2008, 06:56 PM   #6
pieterdhondt
Thread Starter
macrumors newbie
 
Join Date: Mar 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)
pieterdhondt is offline   0 Reply With Quote
Old Mar 24, 2008, 07:55 PM   #7
admanimal
macrumors 68040
 
Join Date: Apr 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.
admanimal is offline   0 Reply With Quote
Old Mar 24, 2008, 08:01 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
View TCP/IP Activity? ArtOfWarfare Mac Programming 5 Mar 9, 2014 04:36 AM
iPhone: TCP/IP over SMS (NOT SMS over TCP/IP)? N64 Jailbreaks and iOS Hacks 3 Jan 1, 2014 03:29 PM
Resolved: Mysterious TCP block? Let's Sekuhara! Mac Basics and Help 13 Dec 14, 2013 12:47 PM
Amazon S3 Constant TCP polling Lunchb0x8 OS X 10.8 Mountain Lion 0 Oct 11, 2012 03:31 AM

Forum Jump

All times are GMT -5. The time now is 09:27 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC