|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
[Java] Socket
...stavo cercando di implementare un server in grado di instaurare una piccola comunicazione con piu' client...questo server di base dovrebbe riuscire non solo a mandare e ricevere messaggi ma anche oggetti di natura diversa...il codice di partenza da me usato è :
Codice:
public class ObjectServer { public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); File file = new File("C:/test.mpg"); while (true) { System.out.println("Listening"); Socket sock = ssock.accept(); ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream()); ostream.writeObject(file); ostream.close(); sock.close(); } } } Codice:
public class ObjectClient { public static void main(String[] args) throws Exception { Socket sock = new Socket("localhost", 1234); ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); Object object = ois.readObject(); if(object == File.class) System.out.println("is file"); if(object == String.class) System.out.println("is string"); System.out.println(object.getClass()); ois.close(); sock.close(); } } ...grazie...ciao... |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
Puoi gestire anche in InputStream lato server e un OutputStream lato client, quello che scrivi da una parte lo leggi dall'altra.
|
![]() |
![]() |
![]() |
#3 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
|
![]() |
![]() |
![]() |
#4 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...ho agganciato i due stream ai metodi ed ho modificato il tutto per inserirlo in un while infinito...ma nell'invio dal client al server si verifica un "Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error"...
Codice:
public class ObjectServer { public static void main(String args[]) throws Exception { boolean alwaysTrue = true; ServerSocket ssock = new ServerSocket(1234); ObjectInputStream ois = null; Socket sock = null; while (alwaysTrue) { sock = ssock.accept(); ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream()); ostream.writeObject("server send"); ostream.close(); if(!sock.isClosed()) { ois = new ObjectInputStream(sock.getInputStream()); Object object = ois.readObject(); System.out.println("server recive : "+object.getClass()); } } sock.close(); } } Codice:
public class ObjectClient { public static void main(String[] args) throws Exception { boolean alwaysTrue = true; Socket sock = new Socket("localhost", 1234); ObjectInputStream ois = null; while(alwaysTrue){ ois = new ObjectInputStream(sock.getInputStream()); Object object = ois.readObject(); System.out.println("client recive : "+object.getClass()); ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream()); ostream.writeObject("client send"); ostream.close(); } ois.close(); sock.close(); } } ...ciao... |
![]() |
![]() |
![]() |
#5 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...trovato soluzione al mio cruccio...
Codice:
public class ObjectServer { public static void main(String[] arg) { MessageData messagge; try{ ServerSocket socketConnection = new ServerSocket(1234); while (true) { System.out.println("Server Waiting"); Socket pipe = socketConnection.accept(); System.out.println(pipe.getInetAddress()); System.out.println(pipe.getRemoteSocketAddress()); ObjectInputStream serverInputStream = new ObjectInputStream(pipe.getInputStream()); ObjectOutputStream serverOutputStream = new ObjectOutputStream(pipe.getOutputStream()); messagge = (MessageData)serverInputStream.readObject(); messagge.setRequest("resp file"); messagge.setObject(new File("c:/test.mpg")); serverOutputStream.writeObject(messagge); serverInputStream.close(); serverOutputStream.close(); } }catch(Exception e){ System.out.println(e); } } } Codice:
public class ObjectClient { public static void main(String[] arg) { try { MessageData message = new MessageData(); message.setRequest("req file"); message.setObject(new File("c:/test.mpg")); Socket socketConnection = new Socket("localhost", 1234); ObjectOutputStream clientOutputStream = new ObjectOutputStream(socketConnection.getOutputStream()); ObjectInputStream clientInputStream = new ObjectInputStream(socketConnection.getInputStream()); clientOutputStream.writeObject(message); message = (MessageData)clientInputStream.readObject(); System.out.println("request : " + message.getRequest()); File file = (File) message.getObject(); System.out.println(file.length()); System.out.println(file.getName()); System.out.println(file.getParentFile()); clientOutputStream.close(); clientInputStream.close(); }catch(Exception e){ System.out.println(e); } } } ...grazie ciao... |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
l'approccio che usi puo' funzionare, ma se il tuo scopo è passare oggetti java e in generale usare un approccio a oggetti alla programmazione di rete puoi usare RMI che è da anni stabile e maturo.
|
![]() |
![]() |
![]() |
#7 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
|
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
A proposito di RMI c'è un tutorial di PGI-Bis: qui.
Non è un tutorial da zero su RMI ma può esserti utile, segnalare non nuoce ![]()
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
#9 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...tutorial molto interessante...ho cominciato pero' con qualche guida un po' piu' semplice per prendere mano con il package...ho trovato un esempio su html.it...dopo aver avviato l'rmiregistry al lancio del server si scatena un class not found...
Codice:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.lang.ClassNotFoundException: sample.ISquareRoot Codice:
package sample; import java.rmi.Remote; import java.rmi.RemoteException; public interface ISquareRoot extends Remote { double calculateSquareRoot(double aNumber) throws RemoteException; } Codice:
package sample; import java.net.MalformedURLException; import java.rmi.server.UnicastRemoteObject; import java.rmi.Naming; import java.rmi.RemoteException; public class RMISquareRootServer extends UnicastRemoteObject implements ISquareRoot { public RMISquareRootServer()throws RemoteException { } public double calculateSquareRoot(double aNumber) { return Math.sqrt( aNumber); } public static void main(String[] args) { try { ISquareRoot server = new RMISquareRootServer(); Naming.rebind("//localhost/RMISquareRoot",server); } catch (RemoteException e){e.printStackTrace( );} catch (MalformedURLException e) {e.printStackTrace( );} } } Codice:
package sample; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.net.MalformedURLException; public class RMISquareRootClient { public static void main(String[] args) { int x = Integer.parseInt(args[0]); try { ISquareRoot squareServer = (ISquareRoot) Naming.lookup ("rmi://localhost/RMISquareRoot"); double result = squareServer.calculateSquareRoot(x) ; System.out.println(result); } catch(NotBoundException e) { e.printStackTrace( ); } catch(RemoteException e) { e.printStackTrace( ); } catch(MalformedURLException e) { e.printStackTrace( ); } } } ...grazie ciao... |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
ti manca qualche property da settare, ti consiglio questo link :
http://java.sun.com/docs/books/tutor...i/running.html |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:38.