Allego tutto...tanto è solo una prova....nulla di importante....
Ecco tutto il codice:
Message
Clientcodice:import java.io.Serializable; public class Message implements Serializable { private String message; public Message (String msg) { message = msg; } public String getMessage() { return message; } public void setMessage (String msg) { message = msg; } }
Servercodice:import java.net.*; import java.io.*; class Client { public static void main (String [] args){ try { Client chatclient = new Client(); } catch (Exception e) { System.err.println("Errore di I/O :" + e); } } public static final int porta = 1428; public Message message = new Message (""); public Client () throws Exception { //Apro una socket per leggere e scrivere Socket client = new Socket (InetAddress.getLocalHost() , porta); System.out.println("Connesso con il server!"); ObjectInputStream ois; ObjectOutputStream oos; //Ora leggo e scrivo while (true) { oos = new ObjectOutputStream ( client.getOutputStream() ); EasyIn easy = new EasyIn (); System.out.print("Digita il tuo messaggio: "); message = new Message ( easy.readString() ); oos.writeObject(message); oos.flush(); ois = new ObjectInputStream ( client.getInputStream() ); String INmessage = ( ((Message) ois.readObject()).getMessage() ); System.out.println( INmessage ); /* ois.close(); System.out.println ("Ho chiuso lo stream di Input"); oos.close(); System.out.println ("Ho chiuso lo stream di Output"); */ } } }
Server Threadcodice:import java.net.*; import java.io.*; class Server { public static void main (String [] args){ try { Server chatserver = new Server(); } catch (IOException e) { System.err.println("Errore di IO" + e); } } public static final int porta = 1428; public Server () throws IOException { ServerSocket server = new ServerSocket (porta); while (true) { Socket client = server.accept(); System.out.println("Connection Accepted"); Thread connection = new ServerThread ( client.getInputStream() , client.getOutputStream() ); connection.start(); } } }
EasyIncodice:import java.net.*; import java.io.*; public class ServerThread extends Thread { private ObjectInputStream input; private ObjectOutputStream output; public ServerThread (InputStream is, OutputStream os) { try { input = new ObjectInputStream ( is ); output = new ObjectOutputStream ( os ); } catch ( Exception e) { System.err.println ("Errore nel costruttore del Thread del Server: " + e ); } } public void run () { try { while (true) { System.out.println ("Leggo il messaggio in input e la stampo a video"); String Messaggio = ( ((Message) input.readObject() ).getMessage() ); System.out.println(Messaggio); System.out.println ("Provo a inviare un messaggio"); Message conferma = new Message ("Messaggio ricevuto!"); output.writeObject (conferma); output.flush(); System.out.println ("Svuotato il buffer di output,adesso riparto!"); //output.close(); //input.close(); } } catch (Exception e) { System.err.println ("Errore nel Thread del Server: "+ e ); } } }
Grazie per la disponibilitàcodice:// Simple input from the keyboard for all primitive types. ver 1.0 // Copyright (c) Peter van der Linden, May 5 1997. // corrected error message 11/21/97 // // The creator of this software hereby gives you permission to: // 1. copy the work without changing it // 2. modify the work providing you send me a copy which I can // use in any way I want, including incorporating into this work. // 3. distribute copies of the work to the public by sale, lease, // rental, or lending // 4. perform the work // 5. display the work // 6. fold the work into a funny hat and wear it on your head. // // This is not thread safe, not high performance, and doesn't tell EOF. // It's intended for low-volume easy keyboard input. // An example of use is: // EasyIn easy = new EasyIn(); // int i = easy.readInt(); // reads an int from System.in // float f = easy.readFloat(); // reads a float from System.in import java.io.*; import java.util.*; class EasyIn { static InputStreamReader is = new InputStreamReader( System.in ); static BufferedReader br = new BufferedReader( is ); StringTokenizer st; StringTokenizer getToken() throws IOException { String s = br.readLine(); return new StringTokenizer(s); } boolean readBoolean() { try { st = getToken(); return new Boolean(st.nextToken()).booleanValue(); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readBoolean"); return false; } } byte readByte() { try { st = getToken(); return Byte.parseByte(st.nextToken()); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readByte"); return 0; } } short readShort() { try { st = getToken(); return Short.parseShort(st.nextToken()); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readShort"); return 0; } } int readInt() { try { st = getToken(); return Integer.parseInt(st.nextToken()); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readInt"); return 0; } } long readLong() { try { st = getToken(); return Long.parseLong(st.nextToken()); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readLong"); return 0L; } } float readFloat() { try { st = getToken(); return new Float(st.nextToken()).floatValue(); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readFloat"); return 0.0F; } } double readDouble() { try { st = getToken(); return new Double(st.nextToken()).doubleValue(); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readDouble"); return 0.0; } } char readChar() { try { String s = br.readLine(); return s.charAt(0); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readChar"); return 0; } } String readString() { try { return br.readLine(); } catch (IOException ioe) { System.err.println("IO Exception in EasyIn.readString"); return ""; } } // This method is just here to test the class public static void main (String args[]){ EasyIn easy = new EasyIn(); System.out.print("enter char: "); System.out.flush(); System.out.println("You entered: " + easy.readChar() ); System.out.print("enter String: "); System.out.flush(); System.out.println("You entered: " + easy.readString() ); System.out.print("enter boolean: "); System.out.flush(); System.out.println("You entered: " + easy.readBoolean() ); System.out.print("enter byte: "); System.out.flush(); System.out.println("You entered: " + easy.readByte() ); System.out.print("enter short: "); System.out.flush(); System.out.println("You entered: " + easy.readShort() ); System.out.print("enter int: "); System.out.flush(); System.out.println("You entered: " + easy.readInt() ); System.out.print("enter long: "); System.out.flush(); System.out.println("You entered: " + easy.readLong() ); System.out.print("enter float: "); System.out.flush(); System.out.println("You entered: " + easy.readFloat() ); System.out.print("enter double: "); System.out.flush(); System.out.println("You entered: " + easy.readDouble() ); } }
Alder


Rispondi quotando