1.附件见zeromq-ios.framework百度网盘/iOS/zmq
2.zeromq-ios.framework解压拖进工程文件
3.objc-zmq见百度网盘/iOS/zmq
4.objc-zmq.zip解压拖进工程文件
5.other link flags -lstdc++
6.客户端
#import “ZMQObjC.h" ZMQContext *ctx = [[ZMQContext alloc] initWithIOThreads:]; NSString *endpoint = @"tcp://localhost:5555"; ZMQSocket *requester = [ctx socketWithType:ZMQ_REQ]; BOOL didConnect = [requester connectToEndpoint:endpoint]; if (!didConnect) { NSLog(@"*** Failed to connect to endpoint [%@].", endpoint); return; } int kMaxRequest = ; NSData *request = [@"Hello" dataUsingEncoding:NSUTF8StringEncoding]; for (int request_nbr = ; request_nbr < kMaxRequest; ++request_nbr) { @autoreleasepool { NSLog(@"Sending request %d.", request_nbr); [requester sendData:request withFlags:]; NSLog(@"Waiting for reply"); NSData *reply = [requester receiveDataWithFlags:]; NSString *text = [[NSString alloc] initWithData:reply encoding:NSUTF8StringEncoding]; NSLog(@"Received reply %d: %@", request_nbr, text); } } [ctx closeSockets]; [ctx terminate];
7.服务器端
#import “ZMQObjC.h" ZMQContext *ctx = [[ZMQContext alloc] initWithIOThreads:]; NSString *endpoint = @"tcp://*:5555"; ZMQSocket *responder = [ctx socketWithType:ZMQ_REP]; BOOL didBind = [responder bindToEndpoint:endpoint]; if (!didBind) { NSLog(@"*** Failed to bind to endpoint [%@].", endpoint); return EXIT_FAILURE; } while () { @autoreleasepool { // Wait for next request from client NSData *data = [responder receiveDataWithFlags:]; //NSData *data = [responder receiveWithBuffer:<#(void *)#> length:<#(size_t)#> flags:<#(ZMQMessageReceiveFlags)#>] NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Received request: %@", text); // Do some 'work' NSLog(@"do some work"); sleep (); // Send reply back to client NSString *world = @"World"; NSData *reply = [world dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; BOOL ok = [responder sendData:reply withFlags:]; if (!ok) { NSLog(@"failed to reply"); } } } [ctx terminate];