A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余永健 中级黑马   /  2015-2-8 14:12  /  633 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

问题描述:我想要再ipad程序里面,用代码给打印机发送一段字符串,然后打印。

具体情况是这样的,现在想要用wifi连接到打印机,然后给打印机发送一个字符串去打印。但是问题是不知道如何连接打印机,也不知道如何给打印机发送消息。其中打印机和ipad在同一个局域网下,想用socket通过打印机的ip地址进行传送要打印的内容。
  1. .h 里面
  2. Objective C code
  3. ?
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 6
  10. 7
  11. 8
  12. 9
  13. #import <UIKit/UIKit.h>
  14. #import "AsyncSocket.h"

  15. @interface ViewController : UIViewController<AsyncSocketDelegate>
  16. {
  17.     AsyncSocket *asyncSocket;
  18. }

  19. @end



  20. .m里面
  21. Objective C code
  22. ?
  23. 1
  24. 2
  25. 3
  26. 4
  27. 5
  28. 6
  29. 7
  30. 8
  31. 9
  32. 10
  33. 11
  34. 12
  35. 13
  36. 14
  37. 15
  38. 16
  39. 17
  40. 18
  41. 19
  42. 20
  43. 21
  44. 22
  45. 23
  46. 24
  47. 25
  48. 26
  49. 27
  50. 28
  51. 29
  52. 30
  53. 31
  54. 32
  55. 33
  56. 34
  57. 35
  58. 36
  59. 37
  60. 38
  61. 39
  62. 40
  63. 41
  64. 42
  65. 43
  66. 44
  67. 45
  68. 46
  69. 47
  70. 48
  71. 49
  72. 50
  73. 51
  74. 52
  75. 53
  76. 54
  77. 55
  78. 56
  79. 57
  80. 58
  81. 59
  82. 60
  83. 61
  84. 62
  85. 63
  86. 64
  87. 65
  88. 66
  89. 67
  90. 68
  91. 69
  92. 70
  93. 71
  94. 72
  95. 73
  96. 74
  97. 75
  98. 76
  99. 77
  100. 78
  101. 79
  102. 80
  103. 81
  104. 82
  105. 83
  106. 84
  107. 85
  108. 86
  109. 87
  110. 88
  111. #import "ViewController.h"

  112. @interface ViewController ()

  113. @end

  114. @implementation ViewController

  115. - (void)viewDidLoad {
  116.     [super viewDidLoad];
  117.     //    NSError *err = nil;
  118.     //    if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err])
  119.     //    {
  120.     //        asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
  121.     //
  122.     //        [asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
  123.     //
  124.     //        if (![self SocketOpen:@"192.168.1.105" port:18011])
  125.     //        {
  126.     //            NSMutableString *sendString=[NSMutableString stringWithCapacity:1000];
  127.     //            [sendString appendString:@"非警务健身房"];
  128.     //            NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding];
  129.     //
  130.     //            [asyncSocket writeData:cmdData withTimeout:-1 tag:0];
  131.     //        }
  132.     //        NSLog(@"Error: %@", err);
  133.     //    }
  134. }

  135. -(IBAction)haha:(id)sender{
  136.     asyncSocket=nil;
  137.     NSError *err = nil;
  138.     if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err])
  139.     {
  140.         asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];

  141.         [asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

  142.         if (![self SocketOpen:@"192.168.1.105" port:18011])
  143.         {
  144.             NSMutableString *sendString=[NSMutableString stringWithCapacity:100000];
  145.             [sendString appendString:@"Socket测试成功!!!!"];
  146.             NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding];

  147.             [asyncSocket writeData:cmdData withTimeout:-1 tag:0];
  148.         }
  149.         NSLog(@"Error: %@", err);
  150.     }
  151. }
  152. - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
  153. {
  154.     NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
  155.     [sock readDataWithTimeout:1 tag:0];
  156. }
  157. -(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
  158. {

  159. }
  160. - (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
  161. {
  162.     NSLog(@"onSocket:%p didSecure:YES", sock);
  163. }
  164. - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
  165. {
  166.     NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
  167. }
  168. - (void)onSocketDidDisconnect:(AsyncSocket *)sock
  169. {
  170.     //断开连接了
  171.     NSLog(@"onSocketDidDisconnect:%p", sock);
  172. }
  173. - (void)didReceiveMemoryWarning {
  174.     [super didReceiveMemoryWarning];
  175. }
  176. - (void)viewDidUnload {
  177.     asyncSocket=nil;
  178. }
  179. //打开
  180. - (NSInteger)SocketOpen:(NSString*)addr port:(NSInteger)port
  181. {
  182.     if (![asyncSocket isConnected])
  183.     {
  184.         [asyncSocket connectToHost:addr onPort:port withTimeout:-1 error:nil];

  185.         NSLog(@"connect to Host:%@ Port:%d",addr,port);
  186.     }
  187.     return 0;
  188. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马