Created
February 10, 2014 11:28
-
-
Save Drakulix/8914293 to your computer and use it in GitHub Desktop.
ObjFW Socket Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ObjFWTcpAsyncSocketTest.m | |
// | |
// | |
// Created by Victor Brekenfeld on 10.02.14. | |
// | |
// | |
#import <ObjFW/ObjFW.h> | |
@interface TestApplication : OFObject<OFApplicationDelegate> | |
@end | |
@implementation TestApplication | |
- (void)applicationDidFinishLaunching { | |
OFTCPSocket *socket = [[OFTCPSocket alloc] init]; | |
[socket bindToHost:@"0.0.0.0" port:12345]; | |
[socket listen]; | |
[socket asyncAcceptWithBlock:^bool(OFTCPSocket *socket, OFTCPSocket *acceptedSocket, OFException *exception) { | |
if (exception) { | |
of_log((OFConstantString *)@"Failed to accept connection%@", exception); | |
return NO; | |
} | |
int length = 1; | |
void *buffer = malloc(length); | |
[[acceptedSocket retain] asyncReadIntoBuffer:buffer exactLength:length block:^bool(OFStream *stream, void *buffer, size_t length, OFException *exception) { | |
if (exception) { | |
of_log((OFConstantString *)@"Error reading from tcp Socket: %@ with Exception: %@", socket, exception); | |
free(buffer); | |
//[stream cancelAsyncRequests]; | |
//[stream close]; | |
[stream release]; | |
return NO; | |
} | |
of_log((OFConstantString *)@"%d", *(int8_t *)buffer); | |
return YES; | |
}]; | |
return YES; | |
}]; | |
} | |
@end | |
int main(int argc, char * argv[]) { | |
@autoreleasepool { | |
return of_application_main(&argc, &argv, [TestApplication class]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment