版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、网络编程 CFSocketRefCFSocketCreateCreates a CFSocket object of a specified protocol and type. CFSocketRef CFSocketCreate (CFAllocatorRef allocator,SInt32 protocolFamily, SInt32 socketType, SInt32 protocol, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context );- (void)se
2、tTextInMainThread:(NSString *)text NSRange endRange;endRange.location = chatController.textView.text length;endRange.length = textlength;chatController.textView.text= chatController.textView.text stringByAppendingString:server: stringByAppendingString:text;chatController.textView scrollRangeToVisibl
3、e:endRange;- (void)readStreamchar buffer255;NSAutoreleasePool * pool = NSAutoreleasePoolallocinit;while (recv(CFSocketGetNative(_socket),buffer, sizeof(buffer),0) NSString *s = NSString stringWithUTF8String:buffer;self performSelectorOnMainThread:selector(setTextInMainThread :)withObject:s waitUntil
4、Done:YES;pool release;staticvoid TCPServerConnectCallBack(CFSocketRef socket, CFSocketCallBackType type,CFDataRef address, const void *data, void *info) if (data !=NULL) UIAlertView *alert = UIAlertView alloc initWithTitle:message:连接失败”delegate:nil cancelButtonTitle: 关闭 otherButtonTitles:nil;alert s
5、how;alertrelease;return;TCPClientDemoAppDelegate *delegate = (TCPClientDemoAppDelegate *)info;delegate performSelectorInBackground:selector(readStream) withObject:nil;delegate.connController dismissModalViewControllerAnimated:YES;NSNotificationCenter defaultCenter addObserver:delegate.chatController
6、 selector:selector(keyboardWillShown:) name:UIKeyboardWillShowNotification object:nil;NSNotificationCenter defaultCenter addObserver:delegate.chatControllerselector:selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil;- (void) doConnect / CFSocketContext 存放一些 CFSocketRef 的相关信
7、息CFSocketContext CTX = 0,self, NULL,NULL, NULL;/ CFSocketCreate 函数是用来创建一个CFSocketRef ,第一个参数是表示由系统默认的 allocator 来为 CFSocketRef 分配一个内存空间。/ 第二个参数表示profocolFamily (协议簇)/ 第三个参数表示 socketType( socket 的类型流、数据段、 顺序包 .)/ 第四个参数表示protocol (协议 tcp、udp.)/ 第五个参数表示当 socket 的某个类型 activity 时,调用第 六个参数的函数/ 第六个参数表示当 soc
8、ket 发生第五个参数的类型的动作 时,调用的函数/ 第七个参数表示一个保存CFSocket object 对象的一些相关信息_socket = CFSocketCreate(kCFAllocatorDefault,PF_INET, SOCK_STREAM,IPPROTO_TCP, kCFSocketConnectCallBack, TCPServerConnectCallBack, &CTX);if (NULL =_socket) UIAlertView *alert = UIAlertView alloc initWithTitle:message:创建套接字失败”delegate
9、:nil cancelButtonTitle: 关闭 otherButtonTitles:nil;alert show;alertrelease;/ sockaddr_in 是一个 struct,里面包含 ip、端口等 struct sockaddr_in addr4;memset(&addr4,0, sizeof(addr4);/ memset 表示将地址 addr4 结构里面的前sizeof ()个内存地址里面的内容设置成intoaddr4.sin_len =sizeof(addr4);addr4.sin_family =AF_INET;addr4.sin_port =htons(
10、12345);addr4.sin_addr.s_addr = inet_addr(connController.textField.textUTF8String);/ CFDataCreate() 是用来将一个申请一个 sizeof() 大小的内存空间,并将一个 buffer 中的数据拷贝到新申请的 内存空间中CFDataRef address =CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4);CFSocketConnectToAddress(_socket, address,-1);CFRunLoopR
11、ef cfrl = CFRunLoopGetCurrent();CFRunLoopSourceRef source =CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket,0);CFRunLoopAddSource(cfrl, source,kCFRunLoopCommonModes);CFRelease(source);- (void) sendMessage NSString *stringToSend = chatController.textField.textstringByAppendingString:n;constch
12、ar *data = stringToSendUTF8String;send(CFSocketGetNative(_socket), data,strlen(data) + 1,0);NSRange endRange;endRange.location = chatController.textView.text length;endRange.length = stringToSendlength;chatController.textView.text= chatController.textView.text stringByAppendingString:me: stringByApp
13、endingString:stringToSend;chatController.textView scrollRangeToVisible:endRange;chatController.textField.text = ;recv(), recvfrom()Receive data on a socketPrototypes#include <sys/types.h> #include <sys/socket.h> ssize_t recv(int s, void *buf, size_t len, int flags); ssize_t recvfrom(int
14、s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);DescriptionOnce you have a socket up and connected, you can read incoming data from the remote side using the recv()(for TCP SOCK_STREAM sockets) and recvfrom() (for UDP SOCK_DGRAM sockets).Both functions take the socket
15、 descriptor s, a pointer to the buffer buf, the size (in bytes) of the buffer len, and a set of flags that control how the functions work.Additionally, the recvfrom() takes a struct sockaddr*, from that will tell you where the data came from, and will fill in fromlen with the size of struct sockaddr
16、. (You must also initialize fromlen tobe the size offrom or struct sockaddr.)So what wondrous flags can you pass into this function? Here are some of them, but you should check your local man pages for more information and what is actually supported on your system. You bitwise-or these together, or
17、just set flags to 0 if you want it to be a regular vanilla recv().MSG_OOBReceive Out of Band data. This is how to get data that has been sent to you with the MSG_OOB flag in send(). As the receiving side, you will have had signal SIGURG raised telling you there is urgent data. In your handler for th
18、at signal, you could call recv() with this MSG_OOB flag.MSG_PEEKIf you want to call recv() just for pretend, you can call it with this flag. This will tell you whats waiting in the buffer for when you call recv() for real (i.e. without the MSG_PEEK flag. Its like a sneak preview into the nextrecv() call.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 土壤污染风险评估模型-洞察及研究
- 线虫对重金属污染的耐受性研究-洞察及研究
- 群体动力学在危机情境下的影响-洞察及研究
- 水污染防控技术研究-洞察及研究
- 食品检测无人机在食品安全中的应用-洞察及研究
- 防腐涂料耐候性研究-洞察及研究
- 容灾备份弹性设计-洞察及研究
- 信用评级模型创新策略-洞察及研究
- 社区户外休息活动方案
- 社区送奶茶活动方案
- 功能母料技术解析与应用
- 2025 医药数字化增长策略报告:以用户为中心用内容和服务打造第二增长曲线
- 制冷初级工培训课件
- 2025年初级养老护理员考试理论知识模拟试题(带答案)
- 甘肃教辅管理办法
- 《HJ 212-2025 污染物自动监测监控系统数据传输技术要求》
- 档案管理考试题库及答案
- 中老年健身知识培训课件
- 急性腹痛的鉴别与护理
- 电价分类介绍课件
- 美术山楂树课件
评论
0/150
提交评论