[英]java.io.EOFException while reciving a object. send file from client to server through socket
I a creating a an android app that can share files through socket. 我创建了一个可以通过套接字共享文件的android应用。 First I am creating a Server that will run on every app and if a one app wants to send a file to another user then he can select a server ip address and send file.
首先,我要创建一个将在每个应用程序上运行的服务器,如果一个应用程序想要将文件发送给另一个用户,则他可以选择服务器IP地址并发送文件。
here is the server part 这是服务器部分
public class ServerSocketThread extends Thread {
@Override
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
MessageActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
infoPort.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}});
while (true) {
socket = serverSocket.accept();
// FileTxThread fileTxThread = new FileTxThread(socket);
// fileTxThread.start();
//---------------------------------
ClientRxThread clientRxThread = new ClientRxThread(socket);
clientRxThread.start();
//----------------------------------------
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private class ClientRxThread extends Thread {
Socket socket = null;
ClientRxThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
File file;
ObjectInputStream ois;
ois = null;
InputStream in = null;
byte[] bytes;
FileOutputStream fos = null;
file = new File(getApplicationInfo().dataDir, "test.png");
try {
in = socket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
try {
ois = new ObjectInputStream(in);
} catch (IOException e1) {
System.out.println("Can't get Object Input Stream. ");
e1.printStackTrace();
}
try {
assert ois != null;
bytes = (byte[])ois.readObject();
} catch (ClassNotFoundException | IOException e) {
System.out.println("Can't read Object . ");
bytes= new byte[0];
e.printStackTrace();
}
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
System.out.println("Can't get file output stream . ");
e1.printStackTrace();
}
try {
assert fos != null;
fos.write(bytes);
} catch (IOException e1) {
System.out.println("Can't file output stream write . ");
e1.printStackTrace();
}
finally {
if(fos!=null){
try {
fos.close();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
MessageActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MessageActivity.this,
"Finished",
Toast.LENGTH_LONG).show();
}});
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Here is the client part 这是客户端部分
private class ClientRxThread extends Thread {
String dstAddress;
int dstPort;
ClientRxThread(String address, int port) {
dstAddress = address;
dstPort = port;
}
@Override
public void run() {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
FileTxThread fileTxThread = new FileTxThread(socket);
fileTxThread.start();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
@Override
public void run() {
File file = new File(newImageUri.getPath());
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
final int read = bis.read(bytes, 0, bytes.length);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(bytes);
oos.flush();
socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
FileSharingActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(FileSharingActivity.this,
sentMsg,
Toast.LENGTH_LONG).show();
}});
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I am getting java.io.EOFException of the server in this part. 我在这部分中获取服务器的java.io.EOFException。 Any help would be great.
任何帮助都会很棒。
try {
ois = new ObjectInputStream(in);
} catch (IOException e1) {
System.out.println("Can't get Object Input Stream. ");
e1.printStackTrace();
The peer has closed the socket, which causes end of stream at the receiver, which causes an EOFException
in readObject()
. 对等方已关闭套接字,这导致接收方流结束,这导致
readObject()
的EOFException
。
There are several problems here. 这里有几个问题。 Neither the server nor the client should close
socket
in those finally
blocks: 服务器和客户端都不应该在这些
finally
块中关闭socket
:
finally
block in the server should close serverSocket
, not socket
, and socket should be a local variable in the
accept()` loop, not in the outer scope. finally
块应关闭serverSocket
,而不是socket
,并且socket should be a local variable in the
accept()循环中socket should be a local variable in the
,而不是外部作用域。 It has no meaning once the loop has iterated. socket
in the thread that is started to handle it, not in the code that starts the thread. socket
,而不是在启动线程的代码中关闭socket
。 ObjectOutputStream
, at least not in the code you posted. ObjectOutputStream
,至少在您发布的代码中没有。 If you're not going to use ObjectOutputStream
at the sender, you can't use ObjectInputStream
at the receiver. ObjectOutputStream
,则不能在接收方使用ObjectInputStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.