I use the asyncsocket sample as a starting point to learn more about wlan communication on iPhone.
On the Mac I start a sample server opening port 0. This works, since I can write data with a test client running on the mac.
On the iPhone I think I managed to connect since "streams connected" returns YES.
Then I would like to send data with a syncsocket: (EDITED VERSION WITH COMPLETE CODE)
import "InterfaceTestAppDelegate.h"
import "InterfaceTestViewController.h"
import "AsyncSocket.h"
import "SyncSocket.h"
@implementation InterfaceTestAppDelegate
@synthesize window;
@synthesize viewController;
(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)remoteHost port:(UInt16)remotePort
{
NSLog(@"Socket is connected!");
NSLog(@"Remote Address: %@:%hu", remoteHost, remotePort);
NSString *localHost = [sock localHost];
UInt16 localPort = [sock localPort];
NSLog(@"Local Address: %@:%hu", localHost, localPort);
}
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"application:didFinishLaunchingWithOptions:");
/*
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil;
if (![asyncSocket connectToHost: @"192.168.0.30" onPort: 1234 error: &err])
{
NSLog(@"Error connecting: %@", err);
}
NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"trace 1");
[asyncSocket writeData:data withTimeout:10 tag:0];
NSLog(@"trace 2");
*/
syncSocket = [[SyncSocket alloc] initWithTimeout: 10];
syncSocket.nsLog = YES;
if (![syncSocket connectToHost: @"192.168.0.30" onPort: 12345])
{
NSLog(@"Error connecting syncSocket:");
}
NSData *data = [@"testxyz" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"syncSocket trace 1");
[syncSocket writeData:data];
NSLog(@"syncSocket trace 2");
[window addSubview:viewController.view]; [window makeKeyAndVisible]; return YES; }
It never continues to send the data, the writeData always blocks. The IP 192.168.0.30 is my Mac's IP. I just used any port 12345 now as you suggested above.
But I don't really know what I have to do on the Mac to receive??
As you can see I actually use syncsocket, then it blocks.
I also tried asyncSocket, then I get the message in the asyncsocket class: writeStream Can NOT Accept Bytes
Maybe its that I don't setup the Mac correctly,ie what app do I need to run on the Mac to test? Many thank!