英文:
Socket is closed
问题
我正在尝试通过Socket从安卓手机传输数据到服务器。我阅读到关闭套接字的输入或输出流将关闭另一个流和套接字。我注释掉了给定的代码行。但是日志中出现了错误。
更新
但是,如果我上传的文件大小最多为3 KB,则上传成功。
应用程序错误:
java.net.SocketException: 套接字已关闭
服务器错误:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: 流意外结束
@Override
public void run() {
try {
byte[] request = new byte[4096];
byte[] reply = new byte[8192];
final InputStream inFromClient = sClient.getInputStream();
final OutputStream outToClient = sClient.getOutputStream();
SSLSocket remoteSocket = tlsConnectionService.createSSLSocket(remoteHost, remotePort);
final InputStream inFromServer = remoteSocket.getInputStream();
final OutputStream outToServer = remoteSocket.getOutputStream();
// 用于向服务器上传的新线程
new Thread() {
public void run() {
int bytes_read;
try {
while ((bytes_read = inFromClient.read(request)) != -1) {
String newReq = new String(request);
outToServer.write(newReq.replace(LOCAL_SOCKET_URL, remoteHost).getBytes(), 0, bytes_read);
outToServer.flush();
}
} catch (IOException e) {
if (!(e instanceof SocketException)) {
Log.e(M.CPP, e.toString());
}
}
// try {
// outToServer.close();
// } catch (IOException e) {
// Log.e(M.CPP, e.toString());
// }
}
}.start();
// 当前线程管理从服务器到客户端的流(下载)
int bytes_read;
try {
while ((bytes_read = inFromServer.read(reply)) != -1) {
outToClient.write(reply, 0, bytes_read);
outToClient.flush();
}
} catch (IOException e) {
Log.e(M.CPP, e.toString());
} finally {
try {
remoteSocket.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
//outToClient.close();
sClient.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
英文:
I am trying to transfer data via Socket from android phone to server.I read that Closing either the input or output stream of a socket closes another stream and the socket. And I commented out the given lines. But the error appears in the logs.
UPD
But if I upload a file up to 3 KB, then the upload is successful
ERROR App:
java.net.SocketException: Socket is closed
ERROR Server:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
@Override
public void run() {
try {
byte[] request = new byte[4096];
byte[] reply = new byte[8192];
final InputStream inFromClient = sClient.getInputStream();
final OutputStream outToClient = sClient.getOutputStream();
SSLSocket remoteSocket = tlsConnectionService.createSSLSocket(remoteHost, remotePort);
final InputStream inFromServer = remoteSocket.getInputStream();
final OutputStream outToServer = remoteSocket.getOutputStream();
// a new thread for uploading to the server
new Thread() {
public void run() {
int bytes_read;
try {
while ((bytes_read = inFromClient.read(request)) != -1) {
String newReq = new String(request);
outToServer.write(newReq.replace(LOCAL_SOCKET_URL, remoteHost).getBytes(), 0, bytes_read);
outToServer.flush();
}
} catch (IOException e) {
if (!(e instanceof SocketException)) {
Log.e(M.CPP, e.toString());
}
}
// try {
// outToServer.close();
// } catch (IOException e) {
// Log.e(M.CPP, e.toString());
// }
}
}.start();
// current thread manages streams from server to client (DOWNLOAD)
int bytes_read;
try {
while ((bytes_read = inFromServer.read(reply)) != -1) {
outToClient.write(reply, 0, bytes_read);
outToClient.flush();
}
} catch (IOException e) {
Log.e(M.CPP, e.toString());
} finally {
try {
remoteSocket.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
//outToClient.close();
sClient.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论