Je crée une application Android capable de partager des fichiers via socket. D'abord, je crée un serveur qui fonctionnera sur chaque application et si une application veut envoyer un fichier à un autre utilisateur, il peut sélectionner une adresse IP du serveur et envoyer un fichier.java.io.EOFException lors de la récurrence d'un objet. envoyer un fichier du client au serveur via le socket
est ici la partie serveur
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();
}
}
}
}
Voici la partie client
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();
}
}
}
}
Je reçois java.io.EOFException du serveur dans cette partie. Toute aide est la bienvenue.
try {
ois = new ObjectInputStream(in);
} catch (IOException e1) {
System.out.println("Can't get Object Input Stream. ");
e1.printStackTrace();