之前寫過基於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"
@implementation iphone_socketViewController
- (void)viewDidLoad {
[super viewDidLoad];
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil;
if(![asyncSocket connectToHost:@"192.168.0.113" onPort:25001 error:&err])
{
NSLog(@"Error: %@", err);
}
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
[sock readDataWithTimeout:1 tag:0];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"===%@",aStr);
[aStr release];
NSData* aData= [@"<xml>我喜歡你<xml>" dataUsingEncoding: NSUTF8StringEncoding];
[sock writeData:aData withTimeout:-1 tag:1];
[sock readDataWithTimeout:1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
{
NSLog(@"onSocket:%p didSecure:YES", sock);
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
//中斷連線了
NSLog(@"onSocketDidDisconnect:%p", sock);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
asyncSocket=nil;
}
- (void)dealloc {
[asyncSocket release];
[super dealloc];
}
@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:0x16bd00 didConnectToHost:192.168.0.113 port: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:0x16bd00 willDisconnectWithError:(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"
@implementation iphone_socketViewController
- (void)viewDidLoad {
[super viewDidLoad];
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil;
if(![asyncSocket connectToHost:@"192.168.0.113" onPort:25001 error:&err])
{
NSLog(@"Error: %@", err);
}
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
[sock readDataWithTimeout:1 tag:0];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"===%@",aStr);
[aStr release];
NSData* aData= [@"<xml>我喜歡你<xml>" dataUsingEncoding: NSUTF8StringEncoding];
[sock writeData:aData withTimeout:-1 tag:1];
[sock readDataWithTimeout:1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
{
NSLog(@"onSocket:%p didSecure:YES", sock);
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
//中斷連線了
NSLog(@"onSocketDidDisconnect:%p", sock);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
asyncSocket=nil;
}
- (void)dealloc {
[asyncSocket release];
[super dealloc];
}
@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:0x16bd00 didConnectToHost:192.168.0.113 port: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:0x16bd00 willDisconnectWithError:(null)
2010-12-27 19:14:48.928 iphone.socket[3186:307] onSocketDidDisconnect:0x16bd00
原始碼:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.socket/