iOS 网络编程 GCDasynsocket socket 发送数据
2015-07-06 15:26
681 查看
//
// ViewController.m
// 070604gcdasynsocket
//
// Created by tianshangrenjian on 15/7/6.
// Copyright © 2015年 tianshangrenjian. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<GCDAsyncSocketDelegate>
- (IBAction)btStart:(id)sender;
- (IBAction)sendMsg:(id)sender;
@property (strong ,nonatomic)
GCDAsyncSocket *socket;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host
port:(uint16_t)port
{
NSLog(@"connect to server successful!");
[self.socket
readDataWithTimeout:-1
tag:1];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSLog(@"fuck errro :%@",err);
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *str=[[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
- (IBAction)btStart:(id)sender {
GCDAsyncSocket *socket=[[GCDAsyncSocket
alloc]
initWithDelegate:self
delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0)];
NSString *host=@"127.0.0.1";
[socket
connectToHost:host
onPort:12345
error:nil];
// socket.delegate=self;
self.socket=socket;
}
- (IBAction)sendMsg:(id)sender {
NSString *str=@"up:lsslksks";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:data withTimeout:-1 tag:1];
}
@end
// ViewController.m
// 070604gcdasynsocket
//
// Created by tianshangrenjian on 15/7/6.
// Copyright © 2015年 tianshangrenjian. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<GCDAsyncSocketDelegate>
- (IBAction)btStart:(id)sender;
- (IBAction)sendMsg:(id)sender;
@property (strong ,nonatomic)
GCDAsyncSocket *socket;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host
port:(uint16_t)port
{
NSLog(@"connect to server successful!");
[self.socket
readDataWithTimeout:-1
tag:1];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSLog(@"fuck errro :%@",err);
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *str=[[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
- (IBAction)btStart:(id)sender {
GCDAsyncSocket *socket=[[GCDAsyncSocket
alloc]
initWithDelegate:self
delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0)];
NSString *host=@"127.0.0.1";
[socket
connectToHost:host
onPort:12345
error:nil];
// socket.delegate=self;
self.socket=socket;
}
- (IBAction)sendMsg:(id)sender {
NSString *str=@"up:lsslksks";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
[self.socket writeData:data withTimeout:-1 tag:1];
}
@end
相关文章推荐
- 修改cas-server3.5.1(一) cas取消https方法配置。
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- Nginx为什么比Apache Httpd高效
- 安装java tcpdump
- HDOJ-3549 Flow Problem(网络流)
- Http协议时无状态的,没有记忆功能,连接--传送--断开
- 第 1 节: 1-文本自增演示HttpHandler不记忆状态
- HttpHandler动态生成图片
- HttpResponse
- HttpRequest类
- 第 3 节: 3-Http协议之Request、Response
- HTTP协议分析
- 第 11 节: 11-HttpHandler简介
- TCP/UDP编程
- http协议分析工具
- HDOJ-1532 Drainage Ditches(网络流)
- HttpClient的使用方法
- iOS开发经验技巧之IOS中http请求使用cookie
- iOS 网络编程 socket 发送数据
- HttpClient的使用