Skip to content

Instantly share code, notes, and snippets.

@Drakulix
Created February 10, 2014 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drakulix/8914293 to your computer and use it in GitHub Desktop.
Save Drakulix/8914293 to your computer and use it in GitHub Desktop.
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