Ciao a tutti,
ho 2 applicazioni(1 server e 1 client) e voglio che il server passi una stringa al client. Non so se ho fatto bene la scrittura della stream da parte del server (dateci una controllata), il mio problema e' come far leggere dalla stream il client??
Faccio ancora molta confusione con ste cose....
Grazie
--------------- SERVER ----------------------
codice:
import java.io.*;
import java.net.*;
class SempliceServer{
SempliceServer() throws IOException {
ServerSocket Server = new ServerSocket(2000);
InetAddress IndirizzoServer = InetAddress.getLocalHost();
System.out.println("\nSERVER - HOST: " + IndirizzoServer + " in ascolto sulla PORTA: " + Server.getLocalPort() + "\n");
System.out.println("\nAttendo nuove connessioni....");
Socket Client = Server.accept();
System.out.println("\n\nSi è connesso l' HOST: " + Client.getInetAddress() + " alla PORTA: " + Client.getLocalPort());
/* CREAZIONE STREAM CON CLIENT */
InputStream Input = Client.getInputStream();
OutputStream Output = Client.getOutputStream();
PrintStream PrintStr = new PrintStream(Output);
}
/* METODO PER L'INVIO DI MESSAGGI AL CLIENT */
public void InvioMessaggioClient() throws IOException{
String InputLine = null;
System.out.println("\nInvia il messaggio: \n");
/* PER PRENDERE UN INPUT DA TASTIERA */
BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
InputLine = br.readLine();
PrintStr.println(InputLine);
}
}
public class server{
public static void main(String args[]) throws IOException{
SempliceServer Server = new SempliceServer();
Server.InvioMessaggioClient();
}
}
-------------- CLIENT -----------------------
codice:
import java.io.*;
import java.net.*;
class SempliceClient{
SempliceClient() throws IOException{
InetAddress IndirizzoServer = InetAddress.getByName("192.168.0.1");
InetAddress IndirizzoClient = InetAddress.getLocalHost();
int porta = 2000;
Socket Client = new Socket(IndirizzoServer,porta);
System.out.println("\nCLIENT - HOST: " + IndirizzoClient + " PORTA: " + Client.getLocalPort() + "\n");
System.out.println("\nMi sono connesso all' HOST: " + Client.getInetAddress() + " PORTA: " + Client.getPort());
// LETTURA STREAM ???????????????????????
}
}
public class client{
public static void main(String[]args) throws IOException{
try{
SempliceClient Client = new SempliceClient();
}catch (IOException ex) {
ex.printStackTrace ();
}
}
}