views:

326

answers:

4

Hi All

In my application I fetching the updated data in every 25 second… but some time my application crashes while fetching the updated data.. to resolve this we are planning to use socket connection for live updates

I am tried to find any sample application for socket connection or socket communication in ADC Library platform but found no any sample application related to this

Socket programming is new for me. Can any body help me in socket programming or can provide me some link related to it.

Thanks Amit Battan

A: 

http://beej.us/guide/bgnet/

GoAvs
A: 

The primitives used in Socket programming depend completely on the programming language and platform you are using. Actually a better term would be network programming. A "socket" is a significant entity in the network programming world but it's a big world which includes a host of concepts. For network programming using the C programming language in a Unix-like (Unix, Linux etc.) platform, you might want to consider "Unix Network Programming" by the legendary Richard Stevens.

Gaurav Mathur
He tagged the question Cocoa, so we can assume he's either on Mac OS X or using Cocoa Touch on the iPhone. Both environments are UNIX-like (and Mac OS X, since Leopard, is officially a UNIX).
Peter Hosey
A: 

@Peter Hosey

My crash problem ... i have posted on stackoverflow..

http://stackoverflow.com/questions/1469925/application-crashing-on-getting-updated-information-from-database-using-timer-and

Amit Battan
A: 

I used NSSocketPort and the following code... , we followed this link http://macdevcenter.com/pub/a/mac/2006/11/14/how-to-write-a-cocoa-web-server.html

SERVER CONTROLLER CLASS

#import "ServerController.h"
@implementation ServerController
-(IBAction)startStopConnectionAction:(id)sender{
    if ([[sender title] isEqualToString:@"Start"]) {
     [sender setTitle:@"Stop"];
     NSLog(@"starting connection");

     // allocating and initializing ServerModel class
     if (serverModel) {
      [serverModel release];
     }

     serverModel = [[ServerModel alloc] initWithPortNumber:25240 delegate:self];
    }
   else{
        NSLog(@"stopping connection");
    }
}
@end



SERVER MODEL CLASS

#import "ServerModel.h"
@implementation ServerModel
- (id)initWithPortNumber:(int)pn delegate:(id)delegatedObject{
    NSLog(@"within initWithPortNumber: %d",pn);
    if (self = [super init]) {
     NSLog(@"within IF initWithPortNumber");
     // initializing instance variables
     portNumber = pn;
     delegate = delegatedObject;

     // establishing port to listen
     serverSocketPort = [[NSSocketPort alloc] initWithTCPPort:portNumber];
     int fd = [serverSocketPort socket];
     NSLog(@"fd...%d",fd);
     serverFileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
                  closeOnDealloc:YES];

     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
     [nc addObserver:self
         selector:@selector(newConnection:)
          name:NSFileHandleConnectionAcceptedNotification
        object:nil];

     NSLog(@"nc...%@",nc);
     [serverFileHandle acceptConnectionInBackgroundAndNotify];
    }

    return self;
}
- (void)dealloc{
    NSLog(@"within dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [serverFileHandle release];
    [serverSocketPort release];
    [delegate release];
    [super dealloc];
}

- (void)newConnection:(NSNotification *)notification{
    NSLog(@"within newConnection:");
    NSDictionary *userInfo = [notification userInfo];
    NSLog(@"userInfo obtained- %@",userInfo);
    NSFileHandle *remoteFileHandle = [userInfo objectForKey:
              NSFileHandleNotificationFileHandleItem];

    NSNumber *errorNo = [userInfo objectForKey:@"NSFileHandleError"];
    if( errorNo ) {
     NSLog(@"NSFileHandle Error: %@", errorNo);
     return;
    }

    NSLog(@"11111");
     [serverFileHandle acceptConnectionInBackgroundAndNotify];

    if(remoteFileHandle){
     NSData *dataReceieved = [remoteFileHandle availableData];

     NSString *dataConvertedToString = [[NSString alloc] initWithData:dataReceieved encoding:NSASCIIStringEncoding];

     NSLog(@"dataConvertedToString -%@",dataConvertedToString);
                [remoteFileHandle writeData:dataReceieved];
    }

}
@end

to test this code I executed the telnet command on terminal..., telnet 192.168.0.32 25240.., 192.168.0.32 is my machine IP, and 25240 port number defined in coding...

Amit:~ amitbattan$ telnet 192.168.0.32 25240
Trying 192.168.0.32...
Connected to 192.168.0.32.
Escape character is '^]'.
Testing Amit
Testing Amit
Connection closed by foreign host.

I am sending a message 'Testing Amit' and get back it in terminal .. it is ok.. but after it connection gets closed ... as terminal give response 'Connection closed by foreign host.'

Can anybody suggest me how we keep connected our connection as long as we required it..

Amit Battan