socket通信的json数据传输与获取
本文是基于scoket通信的tcp来进行数据的json格式传输与获取的。
首先,我们先要下载AsyncSockethttps://github.com/robbiehanson/CocoaAsyncSocket类库,将RunLoop文件夹下的AsyncSocket.h, AsyncSocket.m, AsyncUdpSocket.h, AsyncUdpSocket.m 文件拷贝到自己的project中。
然后在项目中添加CFNetwork.framework工具包。
然后开始创建连接,代码如下:
scoket=[[AsyncSocket alloc]initWithDelegate:self]; //初始化
[scoket connectToHost:@"host端口" onPort:port error:nil];
//例如:
[scoket connectToHost:@"192.168.10.128" onPort:4001 error:nil];
连接的具体步骤点击connectToHost即可查看。
创建连接后,导入代理标题头@interface ViewController ()<AsyncSocketDelegate>使用下边代理方法:
代理方法1:查看scoket是否连接成功
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"连接成功");
[scoket readDataWithTimeout:timezone tag:1];//接收数据
//心跳包
_connectTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(longConnectToScoket) userInfo:nil repeats:YES];
}
longConnectToScoket方法的实现如下:
//心跳包长连接
-(void)longConnectToScoket{
[scoket readDataWithTimeout:timezone tag:1];//接收数据
}
接下来是数据的封装、发送与接收:
-(void)clickPublish{
dic=@{
@"user" :@"linda", //用户名
@"museum" :@"1", //博物馆名
@"content" :commentField.text //评论内容
};
//字典转换成字符串
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
str=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//封装数据
NSData *commentData=[str dataUsingEncoding:NSUTF8StringEncoding];
[scoket writeData:commentData withTimeout:10 tag:1];//发送数据
[scoket readDataWithTimeout:30 tag:1];//接收数据
commentField.text=nil;//清空评论栏内容
}
对获取的数据进行处理:
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSLog(@"data:%@",data);
NSString *message=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"message:%@",message);
//把字符串转化字典
NSData *jsonData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic1=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
str1=[dic1 objectForKey:@"content"];//获取键所对应的值
NSLog(@"str1=%@",str1);
//发出通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:str1];
}
//通知接收,其中_allComments是可变数组
-(void)receiveNotify{
//注册通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(message:) name:@"change" object:nil];
}
//注册通知方法
-(void)message:(NSNotification *)note{
NSString *str=[[NSString alloc]init];
str = note.object;
_allComments=[[NSMutableArray alloc]init];
[_allComments addObject:str];
[self start];
}
socket通信的json数据传输与获取的更多相关文章
- Android模拟器的ip获取以及模拟器之间socket通信
Android模拟器的ip获取以及模拟器之间socket通信 http://kalogen.iteye.com/blog/1565507 作者:李波 实现网络五子棋时用到了两个设备 ...
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)
原文:使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置) 在上一篇中说到了Silverlight下的Socket通信,在最后的时候说到本篇将会结合地图. ...
- 网络编程:Http通信与Socket通信
http://note.youdao.com/share/?id=f14d304548003f65e34255d3ddf9df31&type=note 网络编程:Http通信与Socket通信 ...
- 我看不下去鸟。。。。Java和C#的socket通信真的简单吗?
这几天在博客园上看到好几个写Java和C#的socket通信的帖子.但是都为指出其中关键点. C# socket通信组件有很多,在vs 使用nuget搜索socket组件有很多类似的.本人使用的是自己 ...
- Android之Socket通信、List加载更多、Spinner下拉列表
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务 ...
- Android开发--Socket通信
一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是"请求-响应方式",即在请求时 ...
- Socket通信(转)
一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户 ...
- http和socket通信的区别
应用层:HTTP协议(基于传输层的TCP协议,主要解决如何包装数据) 传输层: TCP协议(基于网络层的IP协议).TPC/IP协议(主要解决数据如何在网络中传输) 网络层: IP 协议 socket ...
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...
随机推荐
- CodeForces 697B Barnicle 模拟
强行模拟 纪念一下…… #include<stdio.h> #include<iostream> #include<algorithm> #include<m ...
- 接口post +json +bean
public ReturnBean<DealBean> getMember(String tagtype, String tag) { try { String requestUrl = ...
- SpringMVC利用Hibernate validator做字段验证
1.添加Hiberbate validator相关的jar包 2.字需要验证的formbean 上添加验证的注解,内置注解有: dBean Validation 中内置的 constraint @Nu ...
- SpringMVC的视图解析器
ViewResolver和View介绍 SpringMVC用于处理视图最重要的两个接口是ViewResolver和View.ViewResolver的主要作用是把一个逻辑上的视图名称解析为一个真正的视 ...
- 201312月CCF-2,ISBN号码分析
明天要考CCF啦,偶还是很紧张的.最近看了数据结构,今天才开始上机练习,对,我就是这么懒..废话不多说,我写这篇文章主要是分析CCF编程的小窍门,因为在网上没找到,所以我决定自力更生丰衣足食.!!!! ...
- web上传大文件的配置
1.项目本身的webconfig 在<system.web>字段下 <httpRuntime targetFramework="4.5" requestLeng ...
- bootstrap validator html attributes 选项
常用的html属性:data-fv-message="The username is not valid"data-fv-notempty="true"data ...
- Find and run the whalesay image
Find and run the whalesay image People all over the world create Docker images. You can find these i ...
- Windows命令查看文件MD5码
D:\>certutil -hashfile md5test.txt MD5 MD5 哈希(文件 md5test.txt): d6 f6 bb 38 b5 6b 67 8f 34 9b e4 d ...
- Java Queue 各种方法的区别
再Java里的某些集合类,其实是实现了多个接口的,所以就会同时又多种方法针对同一种操作,比如LinkedList类. 首先看一下java集合类的继承关系图: 这里简单对其重复的方法做点简单的区分. o ...