2014-08-31 6 views
1

iOS 클라이언트가있는 Python tcp 서버가 있습니다. 그것은 데이터를 보내고 받아 들일 수 있습니다. 유일한 문제는 인코딩과 관련이 있습니다. 파이썬 서버에 JPEG를 보내고 서버의 JPEG에 데이터를 쓰려고합니다. jpeg가 계속 손상됩니다.TCP를 통해 Python 서버에 JPEG NSData 보내기

클라이언트의 Obj-C 코드 :

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() 

답변

2

TCP는 스트림 중심의 프로토콜입니다 : 여기

[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]]; 



     }]; 

그리고는 파이썬 (트위스트) 서버 코드입니다. 메시지가 없습니다 (따라서 메시지 경계가 안정되지 않습니다). dataReceived은 몇 바이트로 호출됩니다. 하나 이상은 알지만 실제로는 알 수 없습니다.

dataReceived으로 전달 된 내용을 전체 이미지 데이터로 취급 할 수는 없습니다. 이미지 데이터의 일부 바이트입니다. 기회는 dataReceived이며 이미지 데이터에서 더 많은 바이트가 반복 될 때마다 반복적으로 호출됩니다. 이러한 여러 호출에 전달 된 데이터를 전체 이미지 데이터로 다시 어셈블해야합니다.