通过TCP将JPEG NSData发送到Python服务器
我有一个Python tcp服务器,它有一个iOS客户端。它能够发送和接收数据,我唯一的问题可能是编码。我正在尝试通过TCP将JPEG发送到Python服务器,并将数据写入服务器上的JPEG。jpeg一直在腐蚀 客户机Obj-C代码:通过TCP将JPEG NSData发送到Python服务器,tcp,jpeg,twisted,nsdata,nsoutputstream,Tcp,Jpeg,Twisted,Nsdata,Nsoutputstream,我有一个Python tcp服务器,它有一个iOS客户端。它能够发送和接收数据,我唯一的问题可能是编码。我正在尝试通过TCP将JPEG发送到Python服务器,并将数据写入服务器上的JPEG。jpeg一直在腐蚀 客户机Obj-C代码: [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
[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(twisted)服务器代码:
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()
TCP是一种面向流的协议。它没有消息(因此没有稳定的消息边界)
dataReceived
是用一些字节调用的,至少一个字节,但您不知道还有多少字节
您不能只将传递给dataReceived
的内容视为完整的图像数据。它是来自图像数据的一些字节。很可能会重复调用dataReceived
,每次都会从图像数据中提取更多字节。您必须将传递给这些多个调用的数据重新组合为完整的映像数据