-1
我的代碼在啓動iPhone SDK 3.0之前工作得非常好,我使用套接字函數發送數據和接收數據。我必須做出什麼改變才能使其兼容3.0+。套接字不適用於iPhone SDK 3.0+
-(int)InitSocket:(int)nPort: (NSString*)sServer{
if(sServer == nil)
{
return -1;
}
m_nPort = nPort;
sServerAddress = sServer;
const char* pServer = nil;
pServer = [sServer cStringUsingEncoding:NSASCIIStringEncoding];
m_nSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in servAddr;
memset(&servAddr, 0 , sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(pServer);
servAddr.sin_port = htons(m_nPort);
pfd.fd = m_nSock;
pfd.events = POLLIN | POLLERR| POLLHUP | POLLNVAL;
pfd.revents = 0;
m_nTimeout = -1;
int f = fcntl(m_nSock, F_GETFL, 0);
fcntl(m_nSock, F_SETFL, f | O_NONBLOCK |O_NDELAY);
int opt = 1;
setsockopt(m_nSock, SOL_SOCKET, SO_KEEPALIVE, (const void*)&opt,(socklen_t)sizeof(opt));
struct timeval timeout;
fd_set wset;
FD_ZERO(&wset);
FD_SET(m_nSock, &wset);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
return 0;
}
該代碼發送數據
-(int)SendData:(size_t) nSize: (NSString*) buffer{
int nTotalSent = 0;
const char *pSend = [buffer cStringUsingEncoding:NSASCIIStringEncoding];
while(nTotalSent != nSize)
{
int nSent = send(m_nSock, (const void*)pSend+nTotalSent, nSize-nTotalSent, 0);
if(nSent == -1)
{
return nSent;
}
nTotalSent += nSent;
}
return nTotalSent;
}
請問上面的代碼工作在iPhone SDK 3.0+什麼樣的變化,我需要使用到讓它起作用。
感謝
沒有這些功能是正確的,因爲它在以前工作,代碼沒有變化,但在3.0+我從來沒有得到任何服務器調用的任何響應。 – RVN 2010-02-27 17:21:52