新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
IOS中Socket常用處理方法
這篇文章主要講解了“IOS中Socket常用處理方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習“IOS中Socket常用處理方法”吧!

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)面向各種領(lǐng)域:辦公空間設(shè)計等成都網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷推廣解決方案、網(wǎng)站設(shè)計等建站排名服務(wù)。
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常用處理方法”的內(nèi)容了,經(jīng)過本文的學(xué)習后,相信大家對IOS中Socket常用處理方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
標題名稱:IOS中Socket常用處理方法
文章路徑:http://fisionsoft.com.cn/article/pjgcgo.html


咨詢
建站咨詢
