




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 如何健全辦學管理辦法
- 員額退出機制管理辦法
- 藥品責任追究管理辦法
- 拉薩補貼資金管理辦法
- 肯德基安全培訓課件
- 倒閘操作培訓課件
- 肝膿腫病人護理課件
- 眾安實業培訓課件
- 二附中期末數學試卷
- kbz饋電開關培訓課件
- 聲發射技術裂紋監測
- 社會責任工作管理制度
- 機械CAD-CAM技術課件
- 2024-2025學年廣東省新部編版七年級歷史第二學期期末模擬卷(含答案)
- 2025-2030年環氧丙烷產業市場深度調研及發展趨勢與投資戰略研究報告
- 2024年河南省澠池縣衛生局公開招聘試題帶答案
- 2025年新疆維吾爾自治區公務員錄用考試面試真題試卷:無領導小組討論邊疆穩定與發展試題
- 預防新生兒嗆奶指南
- 2025年高考湖南卷物理真題(解析版)
- 消防課幼兒園課件
- 2025至2030中國汽車物流行業深度發展研究與企業投資戰略規劃報告
評論
0/150
提交評論