简体   繁体   中英

AsyncSockets - ReadToData - Doesn't work like expected

It's my first specific question here on stackoverflow, cause I couldn't found any helpful solutions for my problem yet. I need a low level socket connection between my iPhone and OSX Workstation (as TCP Server), to interchange some media data like pictures or audio files. So I think AsyncSockets is a good choise to get this to work. I've often used it for some tiny byte communication.

My Problem is, that I want to use a kind of a header/protocol to tell the server how much data bytes are still in pipe. A simple communication like "hello world" is working fine, so there are no connection problems.

The mobile device (that wants to send a picture) does the following.

[self setHost:@"172.22.42.207"];
self.socket = [[[AsyncSocket alloc] initWithDelegate:self] autorelease];

NSError *err = nil;
[[self socket] connectToHost:self.host onPort:5009 error:&err];

...

NSData *t = UIImagePNGRepresentation(test);

NSString *header = [NSString stringWithFormat:@"%i", t.length];
NSMutableData *headerData = [[header dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
[headerData appendBytes:[AsyncSocket CRLFData] length:[[AsyncSocket CRLFData] length]];

[[self socket] writeData:headerData withTimeout:-1 tag:0];

The server is listening that way:

AsyncSocket *s = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil;
[s acceptOnPort:5009 error:&err];

if(err)
    NSLog(@"EPIC FAIL...\n%@", err);

....

- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket
{
   NSLog(@"%s", __PRETTY_FUNCTION__);    
   [newSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}

Now: If I use readData:withTimeout:tag it all works like a charm. But once I change the code to readDataToData:withTimeout:tag, to split the header from the other content, the onSocket:didConnectToHost:port: method is never called. Here are some pretty function logs (I placed them in every delegate method)

Client side:
2012-01-31 13:40:32.962 AVMobile[20643:10703] -[SLViewController onSocket:didConnectToHost:port:]
2012-01-31 13:40:32.964 AVMobile[20643:10703] -[SLViewController onSocket:didWriteDataWithTag:]

Server side: 
2012-01-31 13:40:32.961 AVServer[20618:707] -[SLAppDelegate onSocket:didAcceptNewSocket:]

So, next idea... just compare the sending and receiving bytes, so:

Sending:    <33333736 35365cba>
Receiving:  <33333736 35365cba>

Yeah... now my final question: What am I doing wrong!? Why isn't it working out for me :)?

Greetings & thanks! sniperosx

Found a solution:

Just don't use -1 as timeout. With -1 timeout the AsyncSocket is reading data until the other side is closing the connection, so in this range no delegate method is called.

Cheerz sniperosx

[Closed]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM