开发者

writeData on a syncsocket always blocks on iPhone

开发者 https://www.devze.com 2023-02-18 03:38 出处:网络
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 cl

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];
    
        开发者_Python百科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!


For what it's worth, this is specifically how you typically read in some data using AsyncSocket:

-(void)onSocket:(AsyncSocket *)sock
        didReadData:(NSData*)data withTag:(long)tag
    {
    [data getBytes:&getMe length:sizeof(CommProt)];
    // now, you must roll in the next read...
    [sock readDataToLength:sizeof(CommProt) withTimeout:-1 tag:0];  

    // CommProt is your communications protocol, so sizeof(CommProt)
    // is how much to read at a chunk.

    // you can now simply access the fields of getMe,
    // for example getMe.x, getMe.y, getMe.latestValue etc etc.

    // hope it helps!
    }

Of course, you would have previously rolled in the first "primer" read command:

You do that when you connect to a host, hence:

-(void)onSocket:(AsyncSocket *)sock
        didConnectToHost:(NSString *)host port:(UInt16)port
    {
    if ( yourAppSaysItsOkToConnectAtThisMoment == NO )
        {
        [sock disconnect];  // (so easy, AsyncSockets is a masterpiece)
        return;
        }

    // .. blah blah

    // the critical 'primer' read command
    [sock readDataToLength:sizeof(CommProt) withTimeout:-1 tag:0];

    // .. blah blah
    }

Don't forget you must roll in the next read in two places, (a) when you first connect and (b) of course, after each read!

In the example your communications protocol would look like this ...

typedef struct _CommProt        // v.3
    {
    BOOL        pressExplosionButton;
    BOOL        pressFireworksButton;
    float       usersSteering;
    float       usersTemperature;
    float       usersAltitude;
    float       usersAngle;
    }
    CommProt;

Variable like "getMe" in the example would simply look like this:

    CommProt                getMe;
    CommProt                sendMe;

If you are struggling to understand this type of communications protocol, also try this long answer:

Tablet(iPad/Android)-Server Communication Protocol

AsyncSocket is incredibly beautiful, it was written by the mysterious Justin Voss who seemed to drop off the internet after giving it to the world - it's one of the best libraries ever written, it's a masterpiece.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号