Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ObjFW Socket Test
//
// 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