ios实现基于socket tcp/ip的通讯
2012-01-08 02:59
691 查看
原文地址:ios实现基于socket tcp/ip的通讯作者:wiios
之前写过基于http的网络传输层的通讯,现在项目需要实现tcp/ip的通讯协议,通过网络查找了一下,已经有人写好了公开的类库AsyncSocket,下面介绍一下AsyncSocket的使用方法。
AsyncSocket的官方文档:http://code.google.com/p/cocoaasyncsocket/
使用方法如下:
1、创建工程。
2、把AsyncSocket添加到项目中。
3、添加CFNetwork.framework到工程中。
4、实现测试类:
#import<UIKit/UIKit.h>
#import "AsyncSocket.h"
@interface iphone_socketViewController : UIViewController {
AsyncSocket*asyncSocket;
}
@end
相应的方法实现:
#import "iphone_socketViewController.h"
@implementationiphone_socketViewController
- (void)viewDidLoad {
[superviewDidLoad];
asyncSocket= [[AsyncSocket alloc]initWithDelegate:self];
NSError *err= nil;
if(![asyncSocketconnectToHost:@"192.168.0.113"
onPort:25001error:&err])
{
NSLog(@"Error: %@", err);
}
}
- (void)onSocket:(AsyncSocket *)sockdidConnectToHost:(NSString *)hostport:(UInt16)port
{
NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host,port);
[sockreadDataWithTimeout:1 tag:0];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)datawithTag:(long)tag
{
NSString*aStr = [[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"===%@",aStr);
[aStrrelease];
NSData*aData=[@"<xml>我喜欢你<xml>"dataUsingEncoding:NSUTF8StringEncoding];
[sockwriteData:aData withTimeout:-1 tag:1];
[sockreadDataWithTimeout:1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sockdidSecure:(BOOL)flag
{
NSLog(@"onSocket:%p didSecure:YES",sock);
}
- (void)onSocket:(AsyncSocket *)sockwillDisconnectWithError:(NSError *)err
{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock,err);
}
- (void)onSocketDidDisconnect:(AsyncSocket*)sock
{
//断开连接了
NSLog(@"onSocketDidDisconnect:%p",sock);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
- (void)viewDidUnload {
asyncSocket=nil;
}
- (void)dealloc {
[asyncSocketrelease];
[superdealloc];
}
@end
这里只实现了简单的客户端,关于服务器的实现,是采用pathy写的。在源代码中有。
编译运行结果:
服务器端:
bogon:iosworkspace vsp$ ./Servers.py
客户端的IP是: (’192.168.0.169′, 54851)
<xml>我喜欢你<xml>
————–
后来发的数据——-
客户端:
2010-12-27 19:14:47.723 iphone.socket[3186:307] WB:Notice:WinterBoard
2010-12-27 19:14:48.892 iphone.socket[3186:307] onSocket:0x16bd00didConnectToHost:192.168.0.113port:25001
2010-12-27 19:14:48.897 iphone.socket[3186:307]===我是服务器端的数据
2010-12-27 19:14:48.911 iphone.socket[3186:307]===我不喜欢你
2010-12-27 19:14:48.918 iphone.socket[3186:307] onSocket:0x16bd00willDisconnectWithError:(null)
2010-12-27 19:14:48.928 iphone.socket[3186:307]onSocketDidDisconnect:0x16bd00
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.socket/
之前写过基于http的网络传输层的通讯,现在项目需要实现tcp/ip的通讯协议,通过网络查找了一下,已经有人写好了公开的类库AsyncSocket,下面介绍一下AsyncSocket的使用方法。
AsyncSocket的官方文档:http://code.google.com/p/cocoaasyncsocket/
使用方法如下:
1、创建工程。
2、把AsyncSocket添加到项目中。
3、添加CFNetwork.framework到工程中。
4、实现测试类:
#import<UIKit/UIKit.h>
#import "AsyncSocket.h"
@interface iphone_socketViewController : UIViewController {
AsyncSocket*asyncSocket;
}
@end
相应的方法实现:
#import "iphone_socketViewController.h"
@implementationiphone_socketViewController
- (void)viewDidLoad {
[superviewDidLoad];
asyncSocket= [[AsyncSocket alloc]initWithDelegate:self];
NSError *err= nil;
if(![asyncSocketconnectToHost:@"192.168.0.113"
onPort:25001error:&err])
{
NSLog(@"Error: %@", err);
}
}
- (void)onSocket:(AsyncSocket *)sockdidConnectToHost:(NSString *)hostport:(UInt16)port
{
NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host,port);
[sockreadDataWithTimeout:1 tag:0];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)datawithTag:(long)tag
{
NSString*aStr = [[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"===%@",aStr);
[aStrrelease];
NSData*aData=[@"<xml>我喜欢你<xml>"dataUsingEncoding:NSUTF8StringEncoding];
[sockwriteData:aData withTimeout:-1 tag:1];
[sockreadDataWithTimeout:1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sockdidSecure:(BOOL)flag
{
NSLog(@"onSocket:%p didSecure:YES",sock);
}
- (void)onSocket:(AsyncSocket *)sockwillDisconnectWithError:(NSError *)err
{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock,err);
}
- (void)onSocketDidDisconnect:(AsyncSocket*)sock
{
//断开连接了
NSLog(@"onSocketDidDisconnect:%p",sock);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
- (void)viewDidUnload {
asyncSocket=nil;
}
- (void)dealloc {
[asyncSocketrelease];
[superdealloc];
}
@end
这里只实现了简单的客户端,关于服务器的实现,是采用pathy写的。在源代码中有。
编译运行结果:
服务器端:
bogon:iosworkspace vsp$ ./Servers.py
客户端的IP是: (’192.168.0.169′, 54851)
<xml>我喜欢你<xml>
————–
后来发的数据——-
客户端:
2010-12-27 19:14:47.723 iphone.socket[3186:307] WB:Notice:WinterBoard
2010-12-27 19:14:48.892 iphone.socket[3186:307] onSocket:0x16bd00didConnectToHost:192.168.0.113port:25001
2010-12-27 19:14:48.897 iphone.socket[3186:307]===我是服务器端的数据
2010-12-27 19:14:48.911 iphone.socket[3186:307]===我不喜欢你
2010-12-27 19:14:48.918 iphone.socket[3186:307] onSocket:0x16bd00willDisconnectWithError:(null)
2010-12-27 19:14:48.928 iphone.socket[3186:307]onSocketDidDisconnect:0x16bd00
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.socket/
相关文章推荐
- ios实现基于socket tcp/ip的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip的通讯
- ios实现基于socket tcp/ip的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip 的通讯
- ios实现基于socket tcp/ip的通讯
- ios实现基于socket tcp/ip的通讯
- 基于TCP/IP的SOCKET接口实现网络通信
- 第3章 TCP/IP Socket网络通讯------第4章 实现Socket C/S应用程序
- AsyncSocket实现tcp/ip的通讯协议
- Java 基于 TCP/IP 实现 Socket中的多客户端通信
- 使用Net类库,实现的基于TCP/IP简单的通讯框架框架(一)
- Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例
- Java 基于TCP/IP 实现简单的 socket 通信
- java分布式开发TCP/IP NIO无阻塞 Socket((基于消息方式实现系统间的通信) )(转)