iphone服务器状态监控,iphone UDP监听器/服务器 - 没有获得数据回调

#import "SimSocket.h"

void simReceivedDataCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {

// NOTE: I don't know if I'm accessing the address argument properly here, or if it contains useful information!

NSLog(@"SimSocket: simReceivedDataCallback function entry. Socket=%d, address=%s, port=%d", CFSocketGetNative(s), inet_ntoa(((struct sockaddr_in *)address)->sin_addr), htons(((struct sockaddr_in *)address)->sin_port));

// send data to SimSocket delegate here

}

@implementation SimSocket

@synthesize readyToUse, delegate, errors;

- (id)initWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {

if (self = [super init]) {

hostStr = host1;

port = port1;

isPublish = YES;

connectionType = connectionType1;

readyToUse = NO;

int sockType = (connectionType == SimSocketConnectionTcp) ? SOCK_STREAM : SOCK_DGRAM;

sockfd = socket(AF_INET, sockType, 0);

if (sockfd == -1) {

NSLog(@"SimSocket:initWithType: Error with socket() - %s", strerror(errno));

return self;

}

memset((void *)&address_in, 0, sizeof(address_in));

address_in.sin_family = AF_INET;

address_in.sin_port = htons(port);

address_in.sin_addr.s_addr = inet_addr([hostStr UTF8String]);

addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];

readyToUse = YES;

return self;

}

else {

return nil;

}

}

- (id)initPublishWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {

if ([self initWithType:connectionType1 host:host1 port:port1]) {

readyToUse = NO;

NSLog(@"SimSocket:initPublishWithType: Successfully created BSD socket - %d", sockfd);

cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketConnectCallBack, simCallback, NULL);

if (cfsocket == NULL) {

NSLog(@"SimSocket:initPublishWithType: Error with CFSocketCreateWithNative()");

return self;

}

readyToUse = YES;

}

return self;

}

- (id)initSubscribeWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 delegate:(id )delegate1 {

NSLog(@"SimSocket:initSubscribeWithType: starting initialization with delegate %@", delegate1);

if ([self initWithType:connectionType1 host:host1 port:port1]) {

readyToUse = NO;

NSLog(@"SimSocket:initSubscribeWithType: Successfully created BSD socket - %d", sockfd);

delegate = delegate1;

address_in.sin_addr.s_addr = htonl(INADDR_ANY);

addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];

int yes = 1;

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {

NSLog(@"SimSocket:initSubscribeWithType: Error with setsockopt() with SO_REUSEADDR - %s", strerror(errno));

}

if (bind(sockfd, (struct sockaddr*) &address_in, sizeof(address_in)) == -1) {

NSLog(@"SimSocket:initSubscribeWithType: Error with bind() - %s", strerror(errno));

errors = [NSString stringWithFormat:@"Bind Failed - %s. Try using a Different Port number.", strerror(errno)];

close (sockfd);

return self;

}

CFSocketContext context = { 0, (void *)self, NULL, NULL, NULL };

// TCP

if (connectionType == SimSocketConnectionTcp) {

if (listen(sockfd, 10) == -1) {

NSLog(@"SimSocket:initSubscribeWithType: Error with listen() - %s", strerror(errno));

return self;

}

cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketAcceptCallBack, simAcceptCallback, &context);

}

// UDP

else if (connectionType == SimSocketConnectionUdp) {

cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketDataCallBack, simReceivedDataCallback, &context);

}

if (cfsocket == NULL) {

NSLog(@"SimSocket:initSubscribeWithType: Error with CFSocketCreateWithNative()");

return self;

}

CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cfsocket, 0);

CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);

CFRelease(source);

NSLog(@"SimSocket:initSubscribeWithType: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());

readyToUse = YES;

NSLog(@"SimSocket:initSubscribeWithType: Completed socket initialization successfully.");

}

return self;

}

- (void)dealloc {

if (cfsocket) CFRelease(cfsocket);

if (sockfd != -1) close(sockfd);

[errors release];

[super dealloc];

}

-(void)shutdown

{

NSLog(@"SimSocket: shutdown: Am shutting down the socket. sockfd = %d", sockfd);

if (cfsocket) CFRelease(cfsocket);

if (sockfd != -1) close(sockfd);

}

- (void)sendBytes:(const void *)bytes length:(int)length {

if (!readyToUse) {

NSLog(@"SimSocket:sendBytes: socket not ready to use for sending");

return;

}

int bytesSent;

if (connectionType == SimSocketConnectionTcp) {

bytesSent = send(sockfd, bytes, length, 0);

}

else {

bytesSent = sendto(sockfd, bytes, length, 0, (struct sockaddr *)&address_in, sizeof(address_in));

}

if (bytesSent < 0) {

NSLog(@"SimSocket:sendBytes: Oops, error- %s. No bytes sent.", strerror(errno));

}

else if (bytesSent < length) {

NSLog(@"SimSocket:sendBytes: Oops, error- %s. Only %i sent out of %i", strerror(errno), bytesSent, length);

}

else {

NSLog(@"SimSocket:sendBytes: Successfully sent the packet (%d bytes) to %s", length, inet_ntoa(address_in.sin_addr));

}

NSLog(@"SimSocket:sendBytes: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值