0

我有一个作为服务器运行的 .NET 桌面程序,我正在尝试在 iOS 中实现我的客户端程序的一个端口,但是我从 NSSocket 得到了一些奇怪的行为。这是来自服务器的相关代码。

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 ... */
    }
}

我正在使用 XCode 4.2 并针对 iOS 5 在 iOSSimulator 中尝试以下代码。托管模拟器的 Mac 与服务器程序位于同一网络上,我可以运行使用相同硬编码本地 IP 的 Java 客户端,如下所示连接就好了。在 Objective C 代码中,我接收一些数据并将其存储在 NSData 成员属性中,然后一旦打开 Socket,我使用流回调告诉我何时尝试发送数据。

- (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!");
    }
}

在 XCode 控制台中,我看到“Stream is open!”,然后是“Stream has space!”,然后是一个异常,显示“无法完成操作。错误的文件描述符”。之后我在我的服务器代码中放置了一个断点_tcpServer.AcceptSocket(),但它从未命中。似乎流打开了,但套接字从不连接?

4

1 回答 1

-1

尝试检查 [serverUrl host] 是否为 nil。

我也遇到了这个问题,所以我所做的就是http://在 IP 地址 ( URLWithString:@"http://10.0.0.5") 之前添加一个协议字符串。

于 2012-06-04T09:57:53.360 回答