1

iOS クライアントを持つ Python tcp サーバーがあります。データを送受信できますが、私が抱えている唯一の問題はエンコードに関する可能性があります。TCP 経由で JPEG を Python サーバーに送信し、データをサーバー上の JPEG に書き込もうとしています。jpeg が破損し続けます。

クライアント Obj-C コード:

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                           completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                               iv = [[UIImageView alloc] initWithImage:image];


                                                               [iv setFrame:[[self view]frame]];





                                                               ConnectionManager *netCon = [ConnectionManager alloc];
                                                               conMan = netCon;
                                                               [conMan initNetworkCommunication];



                                                               [conMan.outputStream write:(const uint8_t *)[imageData bytes] maxLength:[imageData length]];



        }];

そして、ここにpython(ねじれた)サーバーコードがあります:

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):
        file = open('test.jpeg','w')

        file.write(data)
        file.close()


factory = Factory()

factory.clients=[]


factory.protocol = IphoneChat
reactor.listenTCP(2000, factory)
print "Iphone Chat server started"
reactor.run()
4

1 に答える 1

2

TCP はストリーム指向のプロトコルです。メッセージはありません (したがって、安定したメッセージ境界はありません)。 dataReceivedいくつかのバイトで呼び出されます-少なくとも1つですが、それ以上の数は実際にはわかりません。

dataReceived渡されたものを完全な画像データとして扱うことはできません。画像データから数バイトです。チャンスはdataReceived繰り返し呼び出され、そのたびに画像データからさらにバイトが追加されます。これらの複数の呼び出しに渡されたデータを完全な画像データに再構築する必要があります。

于 2014-08-31T21:17:14.330 に答える