重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“IOS中Socket常用处理方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“IOS中Socket常用处理方法”吧!
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联面向各种领域:办公空间设计等成都网站设计、网络营销推广解决方案、网站设计等建站排名服务。
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常用处理方法”的内容了,经过本文的学习后,相信大家对IOS中Socket常用处理方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!