Hi All,
We are trying to build demo tool by using Network framework API.
Here is the code in Objective-c:
// // main.m // #import <Foundation/Foundation.h> #import <Network/Network.h> char *hostname = ""; char *port = ""; nw_connection_t serverConnection; void stopConn() { nw_connection_cancel(serverConnection); } void connectionDidEnd(NSError *error) { NSLog(@"connectionDidEnd: %@", [error localizedDescription]); stopConn(); } void connectionDidFail(NSError *error) { NSLog(@"connectionDidFail: %@", [error localizedDescription]); stopConn(); } void connectionReady(nw_connection_t connection) { NSString *data = @"somedata"; NSData *rawData = [data dataUsingEncoding:NSUTF8StringEncoding]; nw_connection_send(connection, (dispatch_data_t _Nonnull)rawData, NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, FALSE, ^(nw_error_t _Nullable error) { if (error != NULL) { NSLog(@"connection did send Failed."); connectionDidFail((NSError*)error); return; } NSLog(@"connection did send, data: %@",[[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding]); }); } void setupReceive(nw_connection_t connection) { nw_connection_receive(connection, 1, 65536, ^(dispatch_data_t _Nullable content, nw_content_context_t _Nullable context, bool is_complete, nw_error_t _Nullable error) { nw_retain(context); if (content != NULL) { NSString *data = [[NSString alloc] initWithData:(NSData*)content encoding:NSUTF8StringEncoding]; NSLog(@"setupReceive: Receive Data = %@",data); } if (is_complete) { connectionDidEnd((NSError*)error); } else if (error != NULL) { connectionDidFail((NSError*)error); } else { setupReceive(connection); } }); } int main(int argc, const char * argv[]) { @autoreleasepool { nw_endpoint_t endpoint = nw_endpoint_create_host(hostname, port); serverConnection = nw_connection_create(endpoint, nw_parameters_create_secure_tcp(NW_PARAMETERS_DEFAULT_CONFIGURATION, NW_PARAMETERS_DEFAULT_CONFIGURATION)); nw_retain(serverConnection); nw_connection_set_queue(serverConnection, dispatch_get_main_queue()); nw_connection_set_state_changed_handler(serverConnection, ^(nw_connection_state_t state, nw_error_t _Nullable error) { switch (state) { case nw_connection_state_invalid: NSLog(@"Invalid"); break; case nw_connection_state_waiting: NSLog(@"waiting"); break; case nw_connection_state_preparing: NSLog(@"Preparing"); break; case nw_connection_state_ready: NSLog(@"Client connection ready"); connectionReady(serverConnection); break; case nw_connection_state_failed: NSLog(@"FAILED..."); connectionDidFail((NSError*)error); break; case nw_connection_state_cancelled: connectionDidEnd((NSError*)error); NSLog(@"connection cancelled"); break; default: NSLog(@"Unknown State = %d",state); break; } }); setupReceive(serverConnection); nw_connection_start(serverConnection); dispatch_main(); } return 0; }
Above code is not able to send command to server and always we receive below error
nw_protocol_boringssl_write_frames_block_invoke(892) [C1:1][0x1040682a0] Failed to allocate buffer for external data
Please let us know if we are doing anything wrong here.
Thanks & Regards,
Mohmad Vasim