我正在尝试将图像发送到扭曲的服务器并返回到我的iPhone。我的代码在模拟器上运行,但在iPhone上不起作用。我不知道为什么。我正在做的就是将图像数据发送到服务器,然后立即返回我的iPhone。这是我正在使用的相关代码。
服务器端:
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
#print "data is ", data
self.transport.write(data);
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Server Started"
reactor.run()
客户端:
@interface LoginScreen : UIViewController <NSStreamDelegate> {
}
@property (strong, nonatomic) NSMutableData *outputData;
@property (strong, nonatomic) IBOutlet UIImageView *testImage;
@implementation LoginScreen : UIViewController
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"avis-mbp", 80, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge 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 {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
long len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSData *output = [[NSData alloc] initWithBytes:buffer length:len];
if (nil != output) {
[self.appDel.outputData appendBytes:buffer length:len];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
NSLog(@"Event Ended");
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
break;
}
}
-(IBAction)runNetworkingTest:(id)sender {
[self initNetworkCommunication];
NSData *pictureData = UIImagePNGRepresentation([UIImage imageNamed:@"shalin.jpg"]);
NSMutableData *mutedData = [[NSMutableData alloc] initWithData:pictureData];
[outputStream write:[mutedData bytes] maxLength:[mutedData length]];
}
-(IBAction)testPicture:(id)sender {
UIImage *image = [UIImage imageWithData:self.outputData];
self.testImage.image = image
}
答案 0 :(得分:3)
我找到了解决问题的方法。它与写入服务器时可用的空间有关。 iPhone一次只能写入特定数量的字节,因此我必须通过NSStreamEventHasSpaceAvailable案例来规范代理中写入的数据量。这是缺少的一段代码,它允许您将图像写入服务器并通过TCP连接将其读回客户端:
缺少要放入NSStream代理的代码
case NSStreamEventHasSpaceAvailable:
{
if (self.appDel.willWrite && [self.appDel.inputData length] != 0) {
int bufferSize = 1024;
if ([self.appDel.inputData length] > bufferSize){
NSData *sendData = [self.appDel.inputData subdataWithRange:NSMakeRange(0, bufferSize)];
self.appDel.inputData = [[NSMutableData alloc] initWithData:[self.appDel.inputData subdataWithRange:NSMakeRange(bufferSize, [self.appDel.inputData length] - bufferSize)]];
[outputStream write:[sendData bytes] maxLength:[sendData length]];
} else {
[outputStream write:[self.appDel.inputData bytes] maxLength:[self.appDel.inputData length]];
self.appDel.inputData = [[NSMutableData alloc] init];
}
}
}
运行网络测试的修改版本
-(IBAction)runNetworkingTest:(id)sender {
[self initNetworkCommunication];
self.appDel.willWrite = YES;
NSData *pictureData = UIImagePNGRepresentation([UIImage imageNamed:@"shalin.jpg"]);
[self.appDel.inputData appendData:pictureData];
}
显示图像的代码
-(IBAction)showNetworkingArray:(id)sender {
UIImage *image = [UIImage imageWithData:self.appDel.outputData];
self.testImage.image = image;
}
注意:我一次写入1024个字节。如果你一次写太多的东西,它将无法工作。例如,我尝试了1024 * 8,但是对于单个图像不起作用。但是,当我将缓冲区大小设置为1024时,我能够毫无问题地发送大约10张图像。