My code was working absolutely fine before the launch of iPhone SDK 3.0 , I am using socket functions to send data and receive data. What changes must i make to make it compatible for 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;
}
This Code for sending data
-(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;
}
Will the above code works in iPhone SDK 3.0+ , what changes i need to employ to make it work.
Thanks
You could start by properly checking for errors. All those functions that you use return an error code if something goes wrong and you will never know.