又拍网 for iPhone v1.0

iOS 使用socket连接Internet

Chirpware posted @ 2011年5月18日 22:02 in iphone开发 with tags ios iphone socket tcp udp netw , 8294 阅读

此文来自:http://www.codeios.com/home-space-uid-21-do-blog-id-10.html

1.使用AsyncSocket(http://code.google.com/p/cocoaasyncsocket/)

来做为Socket的一个基础库.

2.在该库里面提供了一个EchoServer,我们可以利用这个程序直接在MAC系统上面运行一个测试服务器起来.

3.根据AsyncSocket(http://code.google.com/p/cocoaasyncsocket/wiki/iPhone)的WIKI将AsyncSocket加入到iPhone的工程里面,记得将TARGET_OS_IPHONE这个宏在工程上面给定义一下.

4.编译工程通过.

5.连接服务器代码:
**********************************************


// 建立一个Socket实体并连接到本地服务器的7777端口
_client = [[AsyncSocket alloc] initWithDelegate:self];
NSError *err = nil;
if (![_client connectToHost:@"127.0.0.1" onPort:7777 withTimeout:1.0f error:&err]) {
    NSLog(@"client net:%@", err);
}

// 添加事件响应函数
A:- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port;
B:- (void)onSocketDidSecure:(AsyncSocket *)sock;
C:- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
D:- (void)onSocketDidDisconnect:(AsyncSocket *)sock;
E:- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;



// 在函数A中请求读取数据, AsyncSocket内部会在有接收到数据的时候调用函数E
[_client readDataWithTimeout:-1 tag:0];

// 函数E被调用过之后这个读取请求就结束了,我们不想让它结束掉, 所以在函数E结尾处加入, 这样读取过程就可以一直持继下去了
[_client readDataWithTimeout:-1 tag:0];

// 在函数A里面将传入的sock给记录下来,这就是我们连接的服务器的socket接口了
_server = sock;

// 发送数据到服务器
NSData* data;
[_server writeData:data withTimeout:-1 tag:1];

**********************************************

6.这样子我们基本就可以实现与服务器进行收发消息的过程了(注意:这些回调函数都是在主线程进行的,并未在其它的线程中)

7.其它的响应事件未去做过多关注, 请自行解决了.

  • 无匹配
diyago 说:
2011年11月26日 15:42

我也是做ios开发的,希望和你交流一下,我怎么才可以联系你呢?希望有你的QQ或者电话。

diyago 说:
2011年11月26日 15:44

@diyago:我也是做ios开发的,希望和你交流一下,我怎么才可以联系你呢?希望有你的QQ或者电话。我的QQ:1287976580,请加我的时候注明一下你是Chirpware。

Jumper Jacket 说:
2019年10月10日 11:03

Great write article!! Considerably, the article is really the freshest on this notable topic. I concur with your conclusions and will certainly thirstily look forward to your forthcoming updates. Saying thanks can not simply be sufficient, for the exceptional lucidity in your writing.

boomessays.com 说:
2019年10月18日 20:17

You might wonder, neden Why do I need a lower level than URLSession at first?. (If you're not wondering, go ahead and pretend.) Great question! We need boomessays.com today. The thing about communicating with URLSession is that it is based on the HTTP network protocol. Communication with HTTP takes place in a request-response manner. This means that most of the network code in most applications follows the same pattern:

essay help expert 说:
2019年12月04日 19:18

This article is really contains lot more information about This Topic. We have read your all the information some points are also good and some usually are awesome. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.

help with assignment 说:
2019年12月04日 19:19

Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic. We have read your all the information some points are also good and some usually are awesome. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.

Kayce Yellowstone Ja 说:
2021年2月03日 22:39

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you

Kayce Yellowstone Ja 说:
2021年2月03日 22:41

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you

GSEB Model Paper Cla 说:
2022年9月04日 19:17

Gujarat Board Model Paper 2023 Class 2 Pdf Download with Answers for Gujarati Medium, English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. GSEB Model Paper Class 2 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

BSEB Matric English 说:
2022年9月28日 02:34

Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English, BSEB Matric English Model Paper all students can download BSEB 10th Class English Model Set 2023 Pdf with answer solutions to practice chapter wise important questions.Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter