TCPsocket.h
// // TCPSocket.h // test // // Created by Aden on 11-3-30. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <sys/socket.h> #import <netinet/in.h> #import <arpa/inet.h> #import <unistd.h> @interface TCPSocket : NSObject { CFSocketRef _socket; //套接字 // NSMutableArray* _bufferArray; //缓冲数据保存 float _recvDt; //接收时间间隔 bool _isDisConnect; //连接是否断开 } @property(nonatomic,readwrite,assign)CFSocketRef socket; @property(nonatomic)bool isDisConnect; @property(nonatomic)float recvDt; //创建 +(id)CreateTCPSocket; -(id)initTCPSocket; //请求连接 -(void)doConnect:(NSString*)address port:(UInt16)port; //发送消息 -(void)sendData:(void*)data; //接收消息 -(void)recvData:(void*)data; //关闭连接 -(void)closedSocket; @end
TCPSocket.m
// // TCPSocket.m // test // // Created by Aden on 11-3-30. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "TCPSocket.h" //#import "SocketData.h" /////////////////////////////////////////////////////////////// //连接回调函数 static void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { if (data != NULL) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"连接失败" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [alert release]; return; } TCPSocket *delegate = (TCPSocket*)info; [delegate performSelectorInBackground:@selector(recvData:) withObject:nil]; } /////////////////////////////////////////////////////////////// #pragma mark TCPSocket @implementation TCPSocket @synthesize socket = _socket; @synthesize isDisConnect = _isDisConnect; @synthesize recvDt = _recvDt; //分配内存 +(id)CreateTCPSocket { return [[[TCPSocket alloc] initTCPSocket] autorelease]; } -(id)initTCPSocket { if( (self=[super init]) ) { CFSocketContext CTX = {0, self, NULL, NULL, NULL}; _socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketConnectCallBack, ConnectCallBack, &CTX); if(_socket==nil) NSLog(@"创建失败"); _recvDt=0.0f; _isDisConnect=false; [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(socketUpdate:) userInfo:nil repeats:YES]; } return self; } //计时刷新 -(void)socketUpdate:(float)dt { _recvDt+=dt; //15秒内未有数据交互 if(_recvDt>15.0) { _isDisConnect=false; } } //请求连接 -(void)doConnect:(NSString*)address port:(UInt16)port { struct sockaddr_in addr4; memset(&addr4, 0, sizeof(addr4)); addr4.sin_len = sizeof(addr4); addr4.sin_family = AF_INET; addr4.sin_port = htons(port); addr4.sin_addr.s_addr = inet_addr([address UTF8String]); CFDataRef addressData = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4)); CFSocketConnectToAddress(_socket, addressData, -1); CFRunLoopRef cfrl = CFRunLoopGetCurrent(); CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket, 0); CFRunLoopAddSource(cfrl, source, kCFRunLoopCommonModes); CFRelease(source); } //发送消息 -(void)sendData:(void*)data { send(CFSocketGetNative(_socket), data, sizeof(data),0); _recvDt=0.0f; } //接收消息 -(void)recvData:(void*)data { char buffer[255]; while (recv(CFSocketGetNative(_socket), buffer, sizeof(buffer), 0)) { NSLog(@"server:%d",buffer); data=buffer; } } //关闭连接 -(void)closedSocket { } -(void)dealloc { [super dealloc]; } @end
浙公网安备 33010602011771号