这篇文章主要讲解了“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常用处理方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。