首页 > 代码库 > 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常用处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。