假如你也是一个java程序员,而你又不是很懂Socket。
下面我的这篇文章也许能帮助你一些。
http://xiva.iteye.com/blog/993336
来自:http://www.iteye.com/topic/993392
首先我们写好上面文章中的server端。
下面我们可以访问一下下面的地址:
http://code.google.com/p/cocoaasyncsocket/
这是一个开源框架。呵,不知道拿到自己程序中使用是否涉及侵权。
但是这句话“The CocoaAsyncSocket project is in the public domain.”是我有信心使用它们的源码,否则只能自己用c来写了,或者使用CFSocket、CFNetwork等类自己来写了。不过也无妨,应在在使用线程的情况下,我们也是可以实现的。
总之,为了开发的便捷,我使用了AsyncSocket这个类,这样可以异步通信。
建立一个基于视图的应用程序,按照http://code.google.com/p/cocoaasyncsocket/wiki/Reference_AsyncSocket
我们将AsyncSocket.h和AsyncSocket.m文件添加到我们的项目中,并且导入CFNetwork.framework<系统包>。这样基本准备工作就做好了。
(添加 CFNetwork.framework的位置:"/System/Library/Frameworks/CFNetwork.framework,如果没有则
/Developer/Platforms/iphoneOS.platform/Developer/SDKs/iphoneOS4.3.sdk/System/Library/Frameworks文件夹下
iphoneOS4.3.sdk根据你的版本的不同而不同)
下面提供我的应用中的代码以及界面图:
Socketdemoviewcontroller.h代码
1 // 2 // SocketDemoViewController.h 3 // SocketDemo 4 // 5 // Created by xiang xiva on 10-7-10. 6 // Copyright 2010 __MyCompanyName__. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 #import "AsyncSocket.h" 11 #define SRV_CONNECTED 0 12 #define SRV_CONNECT_SUC 1 13 #define SRV_CONNECT_FAIL 2 14 #define HOST_IP @"192.168.110.1" 15 #define HOST_PORT 8080 16 17 @interface SocketDemoViewController : UIViewController { 18 19 UITextField *inputMsg; 20 UILabel *outputMsg; 21 AsyncSocket *client; 22 } 23 24 @property (nonatomic, retain) AsyncSocket *client; 25 @property (nonatomic, retain) IBOutlet UITextField *inputMsg; 26 @property (nonatomic, retain) IBOutlet UILabel *outputMsg; 27 28 - (int) connectServer: (NSString *) hostIP port:(int) hostPort; 29 - (void) showMessage:(NSString *) msg; 30 - (IBAction) sendMsg; 31 - (IBAction) reConnect; 32 - (IBAction) textFieldDoneEditing:(id)sender; 33 - (IBAction) backgroundTouch:(id)sender; 34 35 @end
socketdemoviewcontroller.m代码
还是先给出我的界面吧,否则很难懂这些代码
这样大家满意了吧!
好了说了这么多我们还是来看看代码究竟怎么回事吧。
首先从头文件开始看吧,
1,导入头文件#import "AsyncSocket.h",然后是一些宏
2,声明一个AsyncSocket对象,其他就是一些IBoutlet
再次我们看看视图加载,
在这个方法中,首先初始化我们的对象,使用代理的方式。对象显示是self。然后我们便需在我们的类中实现它的各种方法,来得到各种我们想得到的。
client = [[AsyncSocket alloc] initWithDelegate:self];
下面就是连接服务器了,
[client connectToHost:hostIP onPort:hostPort error:&err]
并且当client不为空时,我们就读取服务器的信息
[client readDataWithTimeout:-1 tag:0];
其他的代码除了asyncSocket代理外都是我们所熟悉的。
1 - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{ 2 [client readDataWithTimeout:-1 tag:0]; 3 } 4 5 - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err 6 { 7 NSLog(@"Error"); 8 } 9 10 - (void)onSocketDidDisconnect:(AsyncSocket *)sock 11 { 12 NSString *msg = @"Sorry this connect is failure"; 13 [self showMessage:msg]; 14 [msg release]; 15 client = nil; 16 } 17 18 - (void)onSocketDidSecure:(AsyncSocket *)sock{ 19 20 } 21 22 - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ 23 24 NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 25 NSLog(@"Hava received datas is :%@",aStr); 26 self.outputMsg.text = aStr; 27 [aStr release]; 28 [client readDataWithTimeout:-1 tag:0]; 29 }
到此就结束了。
其他要说的,明天再完善吧。