001 /* Send TCP transport data packet */ 002 void 003 tcp_data_send(NSOutputStream *os, void *data, int length) 004 { 005 int sent, total = 0; 006 while (total < length) { 007 sent = [os write:data + total maxLength:length - total]; 008 if (sent < 0) { 009 error("send: %s\n", strerror(errno)); 010 return; 011 } 012 total += sent; 013 } 014 } 015 016 /* Receive TCP transport data packet */ 017 STREAM 018 tcp_data_recv(NSInputStream *is, void *data, uint32 length) 019 { 020 int rcvd = 0; 021 022 while (length > 0) 023 { 024 rcvd = [is read:data maxLength:length]; 025 if (rcvd < 0) 026 { 027 error("recv: %s\n", strerror(errno)); 028 return NULL; 029 } 030 else if (rcvd == 0) 031 { 032 error("Connection closed\n"); 033 return NULL; 034 } 035 036 data += rcvd; 037 length -= rcvd; 038 } 039 040 return data; 041 } 042 043 /* Establish a TCP connection */ 044 BOOL 045 tcp_establist_connect(NSInputStream *is, NSOutputStream *os, const char *server, int tcpPort) 046 { 047 is = nil; 048 os = nil; 049 CFReadStreamRef cfis = nil; 050 CFWriteStreamRef cfos = nil; 051 volatile ConnectionErrorCode errorCode; 052 053 CFStreamCreatePairWithSocketToHost(NULL, 054 CFStringCreateWithCString(NULL, server, kCFStringEncodingASCII), 055 tcpPort, 056 &cfis, 057 &cfos); 058 059 is = (NSInputStream *)cfis; 060 os = (NSOutputStream *)cfos; 061 062 if (is == nil || os == nil) 063 { 064 errorCode = ConnectionErrorGeneral; 065 return False; 066 } 067 068 [is open]; 069 [os open]; 070 071 // Wait until the output socket can be written to (this is the alternative to 072 // letting NSOutputStream block later when we do the first write:) 073 time_t start = time(NULL); 074 int timedOut = False; 075 while (![os hasSpaceAvailable] && !timedOut && errorCode != ConnectionErrorCanceled) 076 { 077 usleep(1000); // sleep for a millisecond 078 timedOut = (time(NULL) - start > TIMOUT_LENGTH); 079 } 080 081 if (timedOut == True) 082 { 083 errorCode = ConnectionErrorTimeOut; 084 return False; 085 } 086 else if (errorCode == ConnectionErrorCanceled) 087 { 088 return False; 089 } 090 091 [is setDelegate:self]; 092 [is scheduleInRunLoop:inputRunLoop forMode:NSDefaultRunLoopMode]; 093 094 return True; 095 } 096 097 char * 098 tcp_get_address(NSOutputStream *os) 099 { 100 CFWriteStreamRef stream; 101 CFSocketNativeHandle socket; 102 CFDataRef data; 103 104 stream = (CFWriteStreamRef)os; 105 data = CFWriteStreamCopyProperty(stream, kCFStreamPropertySocketNativeHandle); 106 socket = *(CFSocketNativeHandle *)CFDataGetBytePtr(data); 107 108 char *ipaddr = malloc(32); 109 struct sockaddr_in sockaddr; 110 socklen_t len = sizeof(sockaddr); 111 if (getsockname(socket, (struct sockaddr *) &sockaddr, &len) == 0) 112 { 113 unsigned char *ip = (unsigned char *) &sockaddr.sin_addr; 114 sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); 115 } 116 else 117 strcpy(ipaddr, "127.0.0.1"); 118 return ipaddr; 119 } 120 121 // Invoked on incoming data arrival, starts the processing of incoming packets 122 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)streamEvent 123 { 124 //... 125 }
IOS中Socket常用处理
原创
©著作权归作者所有:来自51CTO博客作者wcrane的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:Push时隐藏下面的TabBar
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
jmeter 中groovy常用处理脚本
jmeter groovy
jmeter groovy -
freemarker中list常用处理方法
在freemarker中list是经常用到的
freemarker list 数据库 存储 #if -
iptables 常用处理动作
在iptables中,-j 参
封包 子程序 服务器 -
XML数据格式常用处理方法
xml
Java xml 处理方法 数据格式 -
C字符串常用处理函数
<br />本章集中讨论字符串操作,包括拷贝字符串,拷贝字符串的一部分,比较字符串,字符串右
c character string include function -
Json数据格式常用处理方法
json
java json 处理方法 数据格式