我正在尝试使用套接字编程从我的Android手机上接收图片。
我使用此代码但第四行中的in.read()始终返回-1
我该如何解决这个问题?
客户代码:
try {
x = new byte[picLength2];
int current = 0;
int byteRead = in.read(x, 0, x.length);
current = byteRead;
do {
byteRead = in.read(x, current, (x.length - current));
if (byteRead >0) {
current += byteRead;
}
} while (byteRead > -1);
}catch (IOException e){
e.printStackTrace();
}
服务器代码:
try {
File myFile = new File("C:/onlinegame/null.jpg");
mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.write(mybytearray);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}