|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Apr 2007
Messaggi: 153
|
[Java] Invio di oggetti tramite socket
Ho scritto un semplice client e server che comunicano su socket tcp, copiandolo da uno dei tanti esempi che si trovano nella rete.
Il sistema mi invia tramite objectInput/outputstream un file dal client al server. Vorrei però inviare un oggetto diverso, che al suo interno ha un file e anche altre variabili. Quando tento di inviarlo ho un errore ClassNotFoundException. Ho copiato la classe dell' oggetto che invio sia sul client che sul server, solo sono in due package che si chiamano in modo diverso. So che l' invio di oggetti forse sarebbe più corretto con RMI ma vorrei capire bene il funzionamento dei Socket/ServerSocket . Dove sbaglio? Perché ho quell'errore? Posto il codice dei thread, naturalmente ci sono altre classi in gioco. Codice del thread del server Codice:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tcpservermultithread; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.Socket; import java.util.Date; /** * * @author Daniele */ class FileStorager implements Worker { private String remoteFileName; private Date remoteFileLastModified; private String remoteFilePath; private long remoteFileLength; private boolean isDirectory; private String remoteCompleteFileName; private ObjectInputStream in; private ObjectOutputStream out; public void work(Socket s) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(s.getOutputStream()); in = new ObjectInputStream( s.getInputStream()); out = new ObjectOutputStream(s.getOutputStream()); FileInfo info = (FileInfo) in.readObject(); this.remoteFileName=info.getName(); this.remoteFileLastModified=info.getLastModified(); this.remoteFilePath=info.getPath(); this.remoteFileLength=info.getLength(); this.isDirectory=info.getIsDirectory(); remoteCompleteFileName= info.getPath()+File.separator+info.getName(); if(isDirectory) { File localDir=new File(remoteCompleteFileName); if (!localDir.exists()) { if (!localDir.mkdirs()) { send(out,new StoragerResult(StoragerResult.RESULT.CREATIONDIRECTORYFAILED, remoteCompleteFileName)); } else { send(out,new StoragerResult(StoragerResult.RESULT.FILEEXIST, remoteCompleteFileName)); } } else { send(out,new StoragerResult(StoragerResult.RESULT.DIRECTORYEXIST, remoteCompleteFileName)); } s.close(); } else { store(); s.close(); } } private void store() { File localFile = new File(remoteCompleteFileName); long localFileLength= localFile.length(); Date localFileLastModified= new Date(localFile.lastModified()); boolean localFileExist= localFile.exists(); boolean getFile=false; if(localFileExist) { if(localFileLastModified.before(remoteFileLastModified) || localFileLength != remoteFileLength) { getFile=true; } else { getFile=false; } } else { getFile=true; } copy(getFile); } private void copyAndRename(File f) { try { FileInputStream fis; FileOutputStream fos; fis = new FileInputStream(f); fos = new FileOutputStream(remoteCompleteFileName + ".part"); byte[] dati = new byte[fis.available()]; fis.read(dati); fos.write(dati); fis.close(); fos.close(); } catch (IOException ex) { send(out,new StoragerResult(StoragerResult.RESULT.COPYFAILED, remoteCompleteFileName)); } try { File fileToDelete = new File(remoteCompleteFileName); fileToDelete.delete(); File filetoRename = new File (remoteCompleteFileName + ".part"); filetoRename.renameTo(new File(remoteCompleteFileName)); send(out,new StoragerResult(StoragerResult.RESULT.UPLOADSUCCESS, remoteCompleteFileName)); } catch (Exception ex) { send(out,new StoragerResult(StoragerResult.RESULT.DELETEERROR, remoteCompleteFileName)); } } private void copy(boolean getFile) { if (getFile) { try { out.writeBoolean(getFile); File f = (File) in.readObject(); copyAndRename(f); } catch (ClassNotFoundException ex) { send(out,new StoragerResult(StoragerResult.RESULT.GEENERICERROR, remoteCompleteFileName)); } catch (IOException ex) { send(out,new StoragerResult(StoragerResult.RESULT.GEENERICERROR, remoteCompleteFileName)); } } else { try { out.reset(); out.writeBoolean(getFile); out.flush(); } catch (IOException ex) { ex.printStackTrace(); } } } private void send(ObjectOutputStream out, Serializable obj) { try { out.reset(); out.writeObject(obj); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Codice del thread del client Codice:
/** * Task that execute the upload of the file * * @author dan */ public class UpdaterTask implements Callable<StoragerResult> { private FileInfo infoFile; private PlayerInfo playerInfo; private StoragerResult result; private File file; /** * Constructor of the UpdaterTask * * @param next * file to upload * @param playerInfo * Player's info (user, passw...) * @param basePath * the base path where store the file */ public UpdaterTask(FileInfo infoFile, File file, PlayerInfo playerInfo) { this.infoFile = infoFile; this.playerInfo = playerInfo; this.file = file; } /** * * @see java.util.concurrent.Callable#call() */ public StoragerResult call() throws Exception { String connection = playerInfo.getHost(); // connection's host String login = playerInfo.getUser(); // connection's user String passw = playerInfo.getPassw(); // connection's password Socket s = new Socket(connection, playerInfo.getPort()); System.out.println(s.getInetAddress().toString()); //ObjectInputStream in = new ObjectInputStream(s.getInputStream()); ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); send(out,infoFile); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); boolean sendFile = in.readBoolean(); if (sendFile) { send(out,infoFile); } StoragerResult result; result = (StoragerResult) in.readObject(); return result; } private void send(ObjectOutputStream out, Serializable obj) { try { out.reset(); out.writeObject(obj); out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:52.