BSD socket accept() fd leak on iphone OS
BSD socket accept() fd leak on iphone OS
- Subject: BSD socket accept() fd leak on iphone OS
- From: Jay Bone <email@hidden>
- Date: Thu, 24 Sep 2009 12:56:40 -0700
Hopefully this is the right mailing list.
lists.apple.com didn't have an iphone networking list.
I'm trying to write a simple "Hello World" type web server using BSD sockets. When I compile and run this code on a linux box it behaves as I expect, close()'ed file descriptors originally returned from accept() calls are re-used on subsequent connections.
When I run this same code on an iPhone, close()'ed fd's are not reused and accept() calls continue to return higher and higher fd's until it reaches around 256 and at that point the application locks up.
Is the following code not correct? What am I missing? (Some error handling omitted)
int connected;
int so_param;
int sock;
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
exit(1);
}
so_param = 1;
if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
&so_param, sizeof(int)) == -1) {
exit(1);
}
if( bind(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) {
exit(1);
}
if (listen(sock, 5) == -1) {
exit(1);
}
while(1)
{
connected = accept(sock, NULL, NULL);
bytes_recieved = recv(connected, recv_data, BUFF_SIZE, 0);
char buff[BUFF_SIZE];
sprintf(buff,
"HTTP/1.1 200 OK\nConnection: Keep-Alive\nContent-Type: text/html\n\n<html><head><title>iPhone web server</title></head><body>Hello world</body></html>") ;
send(connected, buff, strlen(buff), 0);
if( shutdown(connected, SHUT_RDWR) == -1 ) {
// Error
}
// Read rest of packets...
while(1)
{
int bytes = recv( connected, recv_data, BUFF_SIZE, 0 );
if ( bytes > 0 ) {
// could do something with data if we wanted...
} else if ( bytes == 0 ) {
break;
} else {
// Handle Error ...
break;
}
}
if( close( connected ) == -1 ) {
// Handle Error
}
}
Thanks for any info
Jay
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden