I have a .NET desktop program running as a server and I'm trying to implement a port of my client program in iOS, but I'm getting some odd behavior from NSSocket. Here's the relevant code from the server.
public NetworkListener()
{
_Stream = new MemoryStream();
_tcpServer = new TcpListener(System.Net.IPAddress.Any, 8086);
_endPoints = new List<EndPoint>();
}
public void BeginListening()
{
_tcpServer.Start();
_workerThread = new Thread(new ThreadStart(WorkerLoop));
_workerThread.IsBackground = true;
_workerThread.Start();
}
private void WorkerLoop()
{
while(true)
{
_clientSocket = _tcpServer.AcceptSocket();
_networkStream = new NetworkStream(_clientSocket, true);
/* ... rest of method here ... */
}
}
I'm attempting the following code in the iOSSimulator using XCode 4.2 and targeting iOS 5. The Mac that's hosting the simulator is on the same network as the server program and I can run a Java client that uses the same hardcoded local IP as below to connect just fine. In the Objective C code, I'm receiving some data and storing it in an NSData member property, then once the Socket is opened, I use the stream callback to tell me when to try sending data.
- (IBAction)connectToServer
{
NSURL *serverUrl = [NSURL URLWithString:@"10.0.0.5"];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[serverUrl host], 8086, &readStream, &writeStream);
[self setInStream:(NSInputStream *)readStream];
[self setOutStream:(NSOutputStream *)writeStream];
[self.inStream setDelegate:self];
[self.outStream setDelegate:self];
[self.inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inStream open];
[self.outStream open];
NSError *error = [self.outStream streamError];
if (error) {
NSLog(@"outstream error: %@", [error localizedDescription]);
}
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
NSError *error;
if ([aStream isEqual:outStream]) {
switch (eventCode) {
case NSStreamEventHasSpaceAvailable:
NSLog(@"Stream has space!");
[self writeDataToStream];
break;
case NSStreamEventOpenCompleted:
NSLog(@"Stream is open!");
break;
case NSStreamEventErrorOccurred:
error = [self.outStream streamError];
NSLog(@"Stream error occured: %@", [error localizedDescription]);
break;
default:
break;
}
}
}
- (void)writeDataToStream
{
unsigned int byteIndex = 0;
unsigned int length = data.length;
uint8_t *buffer = (uint8_t *)[data bytes];
while (byteIndex <= length) {
unsigned int bufferLength = (length - byteIndex >= 1024) ? 1024 : (length - byteIndex);
uint8_t tempBuffer[bufferLength];
(void)memcpy(tempBuffer, buffer, bufferLength);
if (self.outStream.hasSpaceAvailable) {
bufferLength = [self.outStream write:tempBuffer maxLength:bufferLength];
byteIndex += bufferLength;
} else
NSLog(@"Stream is blocked!");
}
}
In the XCode console, I see "Stream is open!", followed by "Stream has space!", followed by an exception that says "The operation couldn't be completed. Bad file descriptor". I placed a breakpoint in my server code right after _tcpServer.AcceptSocket()
, but it never hits. It seems that the stream opens, but the socket never connects?