无法调用NSStreamEventHasBytesAvailable

时间:2013-08-08 06:21:49

标签: python iphone ios objective-c sockets

我用Python编写了一个服务器(Twisted)。我已连接到服务器并将数据发送到服务器,但我没有从服务器获取任何数据,并且NSStreamEventHasBytesAvailable未被调用。

这就是我连接服务器的方式:

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 3000, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

问题在于:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
     NSString *event;
     switch (streamEvent)
     {
         case NSStreamEventHasBytesAvailable:
         event = @"NSStreamEventHasBytesAvailable";
         if (theStream == inputStream)
         {
             uint8_t buffer[1024];
             int len;
             while ([inputStream hasBytesAvailable])
             {
                NSLog(@"input =====%@",inputStream);

                len = [inputStream read:buffer maxLength:1024];
                NSLog(@"len -=======%d",len);
                if (len > 0)
                {
                    NSMutableString *output = [[NSMutableString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
                    NSLog(@"Received data--------------------%@", output);
                }
             }
        }
        break;
    }
}

这是服务器端代码:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        a = data.split(':')
        print a
        if len(a) > 1:
           command = a[0]
           content = a[1]

           msg = ""
           if command == "msg":
               msg = content
               print msg

    def message(self, message):
        self.transport.write(message + '\n')

        for c in self.factory.clients:
            c.message(msg)

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(650, factory)
print "Iphone Chat server started"
reactor.run()

我已经尝试了很多并用Google搜索,但我找不到任何解决方案。这是在节省我的时间,所以如果有人工作,请指导我并发布示例代码。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我对iphone一无所知,但我发现你的服务器有些问题。我宁愿把它写成

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        a = data.split(':')
        print a
        if len(a) > 1:
           command = a[0]
           content = a[1]

           msg = ""
           if command == "msg":
               msg = content
               self.message(msg)
               print msg

    def message(self, message):
        for c in self.factory.clients:
            c.transport.write(message + '\n')

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(650, factory)
print "Iphone Chat server started"
reactor.run()

当我使用telnet客户端测试时,它运行良好。