服务端以DataOutputStream发送图片数据给iOS客户端,
这是服务端的代码:
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream("F:\\"+PhotoFile+"");
int size = fis.available();
System.out.println("size = "+size);
byte[] data = new byte[size];
fis.read(data);
dos.writeInt(size);
dos.write(data);
dos.flush();
dos.close();
fis.close();
这是客户端的代码:
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"%@", data);
}
- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag
{
self.length += partialLength;
NSLog(@"partialLength---%lu", (unsigned long)partialLength);
[socket readDataToLength:partialLength withTimeout:-1 tag:0];
}