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 &NBSP;STREAM&NBSP;&NBSP;&NBSP;&NBSP;018&NBSP;&NBSP;&NBSP;&NBSP;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,&NBSP;SERVER,&NBSP;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 (+); 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 }
Socket common processing in iOS