#import <Foundation/Foundation.h>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, const char *argv[])
{
	struct sockaddr_in _addr;
	socklen_t len=sizeof(_addr);
	int _fd, newfd;
	int l;
	char buffer[512];
	NSAutoreleasePool *arp=[NSAutoreleasePool new];
	NSLog(@"started");
	_fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	inet_aton("0.0.0.0", &_addr.sin_addr);
	_addr.sin_family=AF_INET;
	_addr.sin_port = htons(4550);
	if(bind(_fd, &_addr, len))
		{
		NSLog(@"could not bind %d due to %s", _fd, strerror(errno));
		return NO;
		}
	NSLog(@"bound");
	if(listen(_fd, 10))
		{
		NSLog(@"could not listen on %d due to %s", _fd, strerror(errno));
		return NO;
		}
	NSLog(@"listening");
	newfd=accept(_fd, &_addr, &len);
	NSLog(@"accepted: %d", _fd);
	NSLog(@"addr=%s", inet_ntoa(_addr.sin_addr));
	while((l=read(newfd, buffer, sizeof(buffer))) > 0)
		{
		NSLog(@"%@", [NSData dataWithBytes:buffer length:l]);
		}
	NSLog(@"could not read on %d due to %s", _fd, strerror(errno));
	[arp release];
	exit(0);
}
