AsyncSocket - Inconsistent Receive ...

57 views
Skip to first unread message

Ashu Joshi

unread,
Mar 5, 2012, 10:18:45 PM3/5/12
to CocoaAsyncSocket
First off - this is a fantastic resource - and much easier to use then
the native implementation. I am using AsyncSocket to connect to a TCP
Server for exchanging data - the TCP Server is running on an embedded
device. My observation is that when I receive data I don't get all of
it - sometimes. I have observed that when I am running the program on
the iPad or iPad Simulator using the WiFi Connection - the tendency to
receive partially is very high. However if I use the iPad Simulator
while the MacBook is connected to the Ethernet port - then it tends to
work more reliably. I have tried queuing more than one read but to no
effect. This inconsistency does not go away. Any ideas on what may be
going on? I have confirmed with WireShark that the TCP Server is
sending all the data - the iPad code is not getting it. What should I
be doing to improve my code - to ensure that it works all the time?


#import "RemoteDeviceManager.h"

@implementation RemoteDeviceManager

@synthesize remoteDeviceAccessSocket = _remoteDeviceAccessSocket;
@synthesize isTheSocketConnectionAlreadyEstablished =
_isTheSocketConnectionAlreadyEstablished;
@synthesize receiveDataFromRemoteDevice =
_receiveDataFromRemoteDevice;

- (AsyncSocket *)remoteDeviceAccessSocket
{
if(_remoteDeviceAccessSocket == nil) _remoteDeviceAccessSocket =
[[AsyncSocket alloc] initWithDelegate:self];
return _remoteDeviceAccessSocket;
}

- (NSMutableData *)receiveDataFromRemoteDevice
{

if(_receiveDataFromRemoteDevice == nil)
_receiveDataFromRemoteDevice = [[NSMutableData alloc] init];
return _receiveDataFromRemoteDevice;
}

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data
withTag:(long)tag
{
NSLog(@"onSocket:didReadData");
// [self.receiveDataFromRemoteDevice appendData:data];
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
self.isTheSocketConnectionAlreadyEstablished = FALSE;
NSLog(@"Socket Disconnected");

[[NSNotificationCenter defaultCenter]
postNotificationName:@"DiscoveredController" object:nil];
}


- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError
*)err
{

// [self.remoteDeviceAccessSocket readDataWithTimeout:-1
// tag:1];
}

- (void)establishInterfaceWithRemoteDevice:(NSString
*)ipAddressOfRemoteDevice
onPortAddress:
(UInt16)portAddressOfRemoteDevice
{
NSError *socketError = nil;

if([self.remoteDeviceAccessSocket
connectToHost:ipAddressOfRemoteDevice

onPort:portAddressOfRemoteDevice
withTimeout:-1
error:&socketError])
{

self.isTheSocketConnectionAlreadyEstablished = TRUE;
NSLog(@"Socket Connection Success!");


UInt8 cmd[7] = {0x00, 0x05,0x31,0x2c,0x2d,0x31,0x2c};


NSMutableData *getAllBundlesCommand = [[NSMutableData alloc]
initWithBytes:cmd length:7];

if (self.isTheSocketConnectionAlreadyEstablished) {
[self.remoteDeviceAccessSocket
writeData:getAllBundlesCommand
withTimeout:2
tag:1];
// [self.remoteDeviceAccessSocket
readDataWithTimeout:-1
// tag:
1];

[self.receiveDataFromRemoteDevice setLength:0];
[self.remoteDeviceAccessSocket readDataWithTimeout:
2
buffer:self.receiveDataFromRemoteDevice
bufferOffset:0
tag:1];
[self.remoteDeviceAccessSocket
disconnectAfterReadingAndWriting];

}
} else {

NSLog(@"Failed connected socket: %@", [socketError
localizedDescription]);
// return status;
}
}
@end


Ashu Joshi

unread,
Mar 10, 2012, 4:17:06 PM3/10/12
to cocoaasy...@googlegroups.com
I figured - may be I don't understand the full thing - but I got it to work reliably. Yes - I need to issue more readDataWithTimeout calls - one on OnSocket:didReadData and on disconnect error calls. BUT more importantly I am NOT disconnecting from the sockets "disconnectedAfterReadingAndWriting" anymore. Commenting that line ensures that didReadData gets called as more data comes in.
Reply all
Reply to author
Forward
0 new messages