ios socket(基础demo)
http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html
clinetSocket 1、viewcontroller.h
@interface ViewController : UIViewController<</span>GCDAsyncSocketDelegate,UITextFieldDelegate> { GCDAsyncSocket *socket; } @property(strong) GCDAsyncSocket *socket @property (strong, nonatomic) IBOutlet UITextField *host; @property (strong, nonatomic) IBOutlet UITextField *message; @property (strong, nonatomic) IBOutlet UITextField *port; @property (strong, nonatomic) IBOutlet UITextView *status; - (IBAction)connect:(id)sender; - (IBAction)send:(id)sender; @end viewcontroller.m //连接服务器 - (IBAction)connect:(id)sender { socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; socket.delegate = self; NSError *err = nil; BOOL connState=[socket connectToHost:host.text onPort:[port.text intValue] error:&err]; if(!connState) { [self addText:err.description]; }else { NSLog(@"连接服务器:%@ 成功",host.text); [self addText:@"打开端口"]; } } -(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { [self addText:[NSString stringWithFormat:@"连接到:%@",host]]; [socket readDataWithTimeout:-1 tag:0]; } - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"socket did is connect:%@",err); } //发送数据 - (IBAction)send:(id)sender { [socket writeData:[message.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; [self addText:[NSString stringWithFormat:@"发送的数据:%@",message.text]]; [message resignFirstResponder]; [socket readDataWithTimeout:-1 tag:0]; } //读取服务器获取的数据 -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *newMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [self addText:[NSString stringWithFormat:@"服务器:%@:%@",sock.connectedHost,newMessage]]; [socket readDataWithTimeout:-1 tag:0]; } #pragma mark - View lifecycle -(void)addText:(NSString *)str { status.text = [status.text stringByAppendingFormat:@"%@\n",str]; } - (void)viewDidLoad { [super viewDidLoad]; //设置默认的服务器地址和端口 host.text = @"192.168.2.107"; port.text = @"54321"; // Do any additional setup after loading the view, typically from a nib. } 2、serviceSocket 1、appdelegate.h #import "GCDAsyncSocket.h" #import "GCDAsyncUdpSocket.h" @interface AppDelegate : NSObject <</span>NSApplicationDelegate,GCDAsyncSocketDelegate,GCDAsyncUdpSocketDelegate> { GCDAsyncSocket *socket; GCDAsyncSocket *s; } @property(strong) GCDAsyncSocket *socket; - (IBAction)listen:(id)sender; @property (unsafe_unretained) IBOutlet NSTextView *status; @property (unsafe_unretained) IBOutlet NSTextField *port; @property (unsafe_unretained) IBOutlet NSTextField *host; @property (assign) IBOutlet NSWindow *window; 2、appdelegate.m @implementation AppDelegate @synthesize status; @synthesize port; @synthesize host; @synthesize window = _window; @synthesize socket; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { //服务器的端口号 port.stringValue = @"54321"; } -(void)addText:(NSString *)str { status.string = [status.string stringByAppendingFormat:@"%@\n",str]; } //开始监听 - (IBAction)listen:(id)sender { NSLog(@" begin listen"); socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *err = nil; if(![socket acceptOnPort:[port integerValue] error:&err]) { [self addText:err.description]; }else { [self addText:[NSString stringWithFormat:@"开始监听%d端口.",port.integerValue]]; } } - (void)socket:(GCDAsyncSocket *)sender didAcceptNewSocket:(GCDAsyncSocket *)newSocket { // The "sender" parameter is the listenSocket we created. // The "newSocket" is a new instance of GCDAsyncSocket. // It represents the accepted incoming client connection. // Do server stuff with newSocket... [self addText:[NSString stringWithFormat:@"建立与%@的连接",newSocket.connectedHost]]; s = newSocket; s.delegate = self; [s readDataWithTimeout:-1 tag:0]; } //向客户端写数据 -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *receive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [self addText:[NSString stringWithFormat:@"%@:%@",sock.connectedHost,receive]]; NSString *reply = [NSString stringWithFormat:@"服务器收到:%@",receive]; [s writeData:[reply dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; [s readDataWithTimeout:-1 tag:0]; } @end
ios socket(基础demo)的更多相关文章
- IOS Socket 02-Socket基础知识
1. 简介 Socket就是为网络服务提供的一种机制 通信的两端都是Socket 网络通信其实就是Socket间的通信 数据在两个Socket间通过IO传输 2. Socket通信流程图 3. 模拟Q ...
- IOS Socket 01-网络协议基础知识
1. 网络参考模型 OSI参考模型 TCP/IP参考模型 2. 七层简述 1)物理层:主要定义物理设备标准,如网线的接 ...
- iOS Socket 整理以及CocoaAsyncSocket、SRWebSocket源码解析(一)
写在准备动手的时候: Socket通讯在iOS中也是很常见,自己最近也一直在学习Telegram这个开源项目,Telegram就是在Socket的基础上做的即时通讯,这个相信了解这个开源项目的也都知道 ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS Socket 03-建立连接与登录
1. 搭建python服务器 这里我们用到python服务器Socket Server.如何运行Server?下面介绍 1)通过百度云下载文件 http://pan.baidu.com/s/1i5yb ...
- iOS系列 基础篇 03 探究应用生命周期
iOS系列 基础篇 03 探究应用生命周期 目录: 1. 非运行状态 - 应用启动场景 2. 点击Home键 - 应用退出场景 3. 挂起重新运行场景 4. 内存清除 - 应用终止场景 5. 结尾 本 ...
- iOS系列 基础篇 04 探究视图生命周期
iOS系列 基础篇 04 探究视图生命周期 视图是应用的一个重要的组成部份,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 以视图的四种状态为基础,我们来系统了解一下视 ...
- iOS系列 基础篇 05 视图鼻祖 - UIView
iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...
- iOS系列 基础篇 06 标签和按钮 (Label & Button)
iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...
随机推荐
- grootJs的vm结构
按看这段代码生成的vm groot.view("myview", function (vm, ve) { vm.say = "hello word!"; }) ...
- XP明年就被停止技术支持,这会带来什么?谈谈如何做决策
XP是MS的一款老牌操作系统,相信大家都不陌生,甚至还有继续使用的人,当然了,在虚拟机里用它也是很好用的,不过,再漂亮的姑娘,也有嫁人的时候,作为XP的父母,MS微软明年四月将停止支持有十多年历史的 ...
- [工具类]DataTable与泛型集合List互转
写在前面 工作中经常遇到datatable与list,对于datatable而言操作起来不太方便.所以有的时候还是非常希望通过泛型集合来进行操作的.所以这里就封装了一个扩展类.也方便使用. 类 方法中 ...
- 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...
- yii2图片上传
yii2利用自带UploadedFile上传图片 public static function uploadFile($name) { $uploadedFile = UploadedFile::ge ...
- BIEE 后台新建分析没有你创建的数据源
(1)登录http://win-5rnnibkasrt:9704/analytics/saw.dll?bieehome 点击“管理” 找到“发出SQL语句”在里面写call sapurgeallca ...
- ZOJ 3430 Detect the Virus
传送门: Detect the Virus ...
- Core Animation编程指南
本文是<Core Animation Programming Guide>2013-01-28更新版本的译文.本文略去了原文中关于OS X平台上Core Animation相关内容.因为原 ...
- RONOJ 6今明的预算方案(有依赖的背包)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...
- IDE 集成开发环境
集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面工具.集成了代码编写功能 ...