nico
unread,Jan 5, 2012, 1:11:09 AM1/5/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to CocoaAsyncSocket
Has anybody else seen hardware specific problems like this?
I'm pinging a wifi device for a list of wifi networks and displaying
them in a tableview. This works fine in the simulator (IOS 5) and an
iphone 3GS (IOS 4.xx). No luck with multiple iPhone 4s. Here's the two
connection scenarios I tried multiple times:
With a connect timeout of -1, never got a connection.
With a connect timeout of 5, got a connection a few times and data a
subset of those times, but usually got a disconnect error (#2)
For reference, here's my code (please disregard not allocating stuff
properly):
#import "NetworkListTableViewController.h"
#import "Constants.h"
#import "DataClass.h"
@implementation NetworkListTableViewController
@synthesize isConnected;
@synthesize socket;
@synthesize tableContents;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
// Initialize the socket
AsyncSocket *_socket = [[[AsyncSocket alloc]
initWithDelegate:self] autorelease];
self.socket = _socket;
self.isConnected = NO;
// Allocate the empty list of WiFi Networks
NSMutableArray *_array = [[[NSMutableArray alloc] init]
autorelease];
self.tableContents = _array;
[super viewDidLoad];
// Attempt to connect to plug
// TODO: If we can't connect in x seconds, print error message?
NSLog(@"Sending isPlug");
// [self send:@"{\"m\":\"isPlug\"}"];
[self connect];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return [self.tableContents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.tableContents objectAtIndex:row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
DataClass* sharedData = [DataClass sharedInstance];
sharedData.ssid = [self.tableContents objectAtIndex:[indexPath
row]];
[self.navigationController popViewControllerAnimated:YES];
[self disconnect];
}
#pragma mark - Handle incoming data from device
- (void) parseDataFromDevice:(NSString *)content {
// Each message from device is a JSON dictionary
NSDictionary *json = [content objectFromJSONString];
// Check if we have the correct info
if([json objectForKey:@"ssid"]){
NSString *ssid = [json objectForKey:@"ssid"];
[self.tableContents addObject:ssid];
NSLog(@"size of data: %d",[self.tableContents count]);
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:
0] withRowAnimation:UITableViewRowAnimationBottom];
}
}
// TODO: refactor into a separate SocketLib.h file
#pragma mark - higher level socket functions
- (void) connect {
[self.socket setDelegate:self];
if(!self.isConnected){
NSError *error = nil;
if (![self.socket connectToHost:HostName onPort:[HostPort
intValue] withTimeout:5 error:&error]){ <-----
NSLog(@"Error connecting: %@", error);
}
} else {
NSLog(@"SocketView:connect: already connected");
}
}
- (void) disconnect {
[self.socket setDelegate:self];
if ( [self.socket isConnected] ){
[self.socket disconnect];
} else {
NSLog(@"SocketView:disconnect: not connected");
}
}
- (void) send:(NSString*)message {
// Send and write
if(self.isConnected){
NSData *out_data = [message
dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:out_data withTimeout:-1 tag: 0];
}
}
- (void) listen {
if(self.isConnected){
[self.socket readDataToData:[AsyncSocket CRLFData]
withTimeout:-1 tag:0];
}
}
#pragma mark - AsyncSocket delegates
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
NSLog (@"onSocketWillConnect:");
return YES;
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
NSLog (@"didWriteDataWithTag: %d", tag);
}
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)in_data
withTag:(long)tag {
NSLog (@"didReadData:%@ withTag:%d", in_data,tag);
NSString *content = [[NSString alloc] initWithBytes:[in_data
bytes] length:[in_data length] encoding: NSUTF8StringEncoding];
NSLog(@"didReadData:content: %@",content);
[self parseDataFromDevice:content];
[self listen];
}
- (void) onSocket: (AsyncSocket*) sock willDisconnectWithError:
(NSError*) err {
NSLog (@"willDisconnectWithError: %d", [err code]);
}
- (void) onSocket: (AsyncSocket*) sock didConnectToHost: (NSString*)
host port: (UInt16) port {
NSLog (@"didConnectToHost: %@ port: %d", host, port);
//self.status.text = @"Connected";
self.isConnected = YES;
//[self performSelectorInBackground:@selector(listen)
withObject:nil];
[self listen];
[self send:@"{\"m\":\"isXXX\",\"foobar\":
\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"}"];
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock {
NSLog (@"onSocketDidDisconnect:");
//self.status.text = @"Disconnected";
self.isConnected = NO;
}