ho ripreso un piccolo esercizio di prova spulciando il forum
----------------------- SERVER ----------------------------
import java.io.*;
import java.net.*;
class SempliceServer{
public 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 */
OutputStream Output = Client.getOutputStream();
PrintStream PrintStr = new PrintStream(Output);
}
/* METODO PER L'INVIO DI MESSAGGI AL CLIENT */
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 serverasd{
public static void main(String args[]) throws IOException{
SempliceServer Server = new SempliceServer();
Server.InvioMessaggioClient();
}
}
------------------------------CLIENT ----------------------------
import java.io.*;
import java.net.*;
class SempliceClient{
SempliceClient() throws IOException{
try{
InetAddress IndirizzoServer = InetAddress.getLocalHost();
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());
ObjectInputStream input = new ObjectInputStream ( Client.getInputStream() );
String messaggio = (String) input.readObject();
System.out.println("Il server ha inviato questo messaggio:");
System.out.println(messaggio);
}catch (Exception e){}
}
}
public class clientasd{
public static void main(String[]args) throws IOException{
try{
SempliceClient Client = new SempliceClient();
}catch (IOException ex) {
ex.printStackTrace ();
}
}
}
----------------------------------------------------------------------
pero' il Server mi da un errore di compilazione alla riga
PrintStr.println(InputLine);
dicendo
..\bin\serverasd.java:26: cannot find symbol
symbol : variable PrintStr
location: class SempliceServer
PrintStr.println(InputLine);
^
1 error
Come mai??
Se escludo quella linea funziona la parte della connessione
![]()