Autor Tópico: [Objective-C] OjbC  (Lida 1071 vezes)

Offline KTachyon

  • Global Moderator
  • Newbie
  • *****
  • Mensagens: 0
  • Karma: +0/-0
    • Ver Perfil
    • http://twitter.com/KTachyon
[Objective-C] OjbC
« em: Janeiro 02, 2006, 06:44:07 pm »
Bem... aki vai um cheirinho de programação em Objective-C, para o ppl interessado em saber como é k é...

O programa é basicamente um servidor (inútil) que recebe mensagens e as envia para o output (com NSLog) e quando recebe a palavra \\'terminate\\', envia a mensagem de terminação de ligação e disconnecta-se.

Código: [Seleccione]
// ServerTest.m



#import <Foundation/Foundation.h>

#import "Server.h"



int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];



Server *test = [[Server alloc] init];



   [test startServer];



   [[NSRunLoop currentRunLoop] run];



[pool release];

return 0;

}

Código: [Seleccione]
// Server.h



#import <Cocoa/Cocoa.h>





@interface Server : NSObject {



}



- (void)startServer;

- (void)spawnChildConnection:(NSNotification *)note;

- (void)processClientData:(NSNotification *)note;



@end

Código: [Seleccione]
//  Server.m



#import "Server.h"





@implementation Server



- (void)startServer

{

   NSSocketPort *sockPort = [[NSSocketPort alloc] initWithTCPPort:7777];

   [sockPort autorelease];



   int socketFD = [sockPort socket];



   NSFileHandle *listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:socketFD];

   [listeningSocket autorelease];



   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];



   [nc addObserver:self

selector:@selector(spawnChildConnection:)

name:NSFileHandleConnectionAcceptedNotification

 object:listeningSocket];



   [listeningSocket acceptConnectionInBackgroundAndNotify];

}



/*

 * This method is invoked in response to

 * NSFileHandleConnectionAcceptedNotification

 */



- (void)spawnChildConnection:(NSNotification *)note

{

   NSFileHandle *connectedSocket = [[note userInfo] objectForKey:NSFileHandleNotificationFileHandleItem];



   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];



   [nc addObserver:self

selector:@selector(processClientData:)

name:NSFileHandleReadCompletionNotification

 object:connectedSocket];



   // Send a message to the client, acknowledging that the connection was accepted



   NSString *serverWelcomeMessage = @"200 KTachyon\\'s UselessServer v0.0.3a Readyn";

   char *chr = (char*) calloc([serverWelcomeMessage length] + 1, sizeof(char));



   [serverWelcomeMessage getCString:chr];



   NSData *ackData = [NSData dataWithBytes:chr  length:[serverWelcomeMessage length]];



   [connectedSocket writeData:ackData];

   [connectedSocket readInBackgroundAndNotify];

}



/*

 * This method is invoked in response to

 * NSFileHandleReadCompletionNotification

 */



- (void)processClientData:(NSNotification *)note

{

   NSData *data = [[note userInfo] objectForKey:NSFileHandleNotificationDataItem];

   NSFileHandle *socket = [note object];



   NSString *str = [[NSString alloc] initWithCString:[data bytes] length:[data length]];



   NSLog(str);



   if ([str hasPrefix:@"terminate"])

   {

 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

 

 NSString *serverTerminationMessage = @"400 KTachyon\\'s UselessServer v0.0.3a Terminates connectionn";

 char *chr = (char*) calloc([serverTerminationMessage length] + 1, sizeof(char));

 

 [serverTerminationMessage getCString:chr];

 

 NSData *terminationMessage = [NSData dataWithBytes:chr length:[serverTerminationMessage length]];

 

 [socket writeData:terminationMessage];

 [socket closeFile];

 

 [nc removeObserver:self

name:NSFileHandleReadCompletionNotification

  object:socket];

   }

   else

 [socket readInBackgroundAndNotify];



   [str release];

}





@end
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49