mercoledì 10 marzo 2010

Spostamento di una parola svolto in classe

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Visualizza {
JFrame f1=new JFrame("trasferimento");
JTextField t1=new JTextField();
JLabel l1=new JLabel();
JButton b1=new JButton("invio");
public Visualizza(){
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new Bottone());
f1.add(BorderLayout.NORTH,t1);
f1.add(BorderLayout.CENTER,l1);
f1.add(BorderLayout.SOUTH,b1);
f1.setSize(300,300);
f1.setVisible(true);
}
public class Bottone implements ActionListener{
public void actionPerformed(ActionEvent e){
String parola =t1.getText();
l1.setText(parola);
}
}
public static void main(String[]args){
Visualizza v1=new Visualizza();
}
}

Modulo Applicativo Reti

import java.io.*;
import java.net.*;
public class Server {
String[] suggerimenti={"studia piu'","e' meglio che esci di casa,pensa alla salute","se non hai dolori accendi un cero alla Madonna"};
public void go(){
try{
ServerSocket s1=new ServerSocket(4242); //indica qual è la porta di comunicazione
while(true){
Socket presa=s1.accept();
PrintWriter scrivi=new PrintWriter(presa.getOutputStream());
String s=getSuggerimenti();
scrivi.println(s);
scrivi.close();
System.out.println(s);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
private String getSuggerimenti(){
int r =(int)(Math.random()*suggerimenti.length);
return suggerimenti [r];
}
public static void main(String[]args){
Server se=new Server();
se.go();
}
}


import java.io.*;
import java.net.*;
public class Cliente {
public void go(){
try{
Socket s = new Socket("192.168.3.17",4242);
InputStreamReader r=new InputStreamReader(s.getInputStream());
BufferedReader reader=new BufferedReader(r);
String suggerimento=reader.readLine();
System.out.println("Suggerimento: "+suggerimento);
reader.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
public static void main(String[]args){
Cliente c=new Cliente();
c.go();
}
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Cliente2 {
JFrame f1=new JFrame("Suggerimenti");
JTextArea a1=new JTextArea();
public void go(){
try{
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.add(a1);
f1.setSize(500,500);
f1.setVisible(true);
Socket s = new Socket("192.168.3.17",4242);
InputStreamReader r=new InputStreamReader(s.getInputStream());
BufferedReader reader=new BufferedReader(r);
String suggerimento=reader.readLine();
a1.setText(suggerimento);
System.out.println("Suggerimento: "+suggerimento);
reader.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
public static void main(String[]args){
Cliente2 c=new Cliente2();
c.go();
}
}

Esercizio sugli stream...svolto in classe

//per intercettare il tipo di errore input-output degli stream
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
public class Stream {
public static void main(String[]args)throws IOException{
/*if(args.length!=4){
System.out.println("Usage: java CambiaFormato ");
System.exit(-1);
}
*/

if(args.length!=4){
String s="Usage:java CambiaFormato ";
throw new IllegalArgumentException(s);
}
FileInputStream FIn=null;
FileOutputStream FOut=null;
InputStreamReader inStream=null;
OutputStreamWriter outStream = null;
BufferedReader buffRead =null;
BufferedWriter buffWriter=null;
// null sta per vuoti
try{
FIn=new FileInputStream(args[0]);
FOut=new FileOutputStream(args[2]);

inStream=new InputStreamReader(FIn,args[1]);
outStream=new OutputStreamWriter(FOut,args[3]);

buffRead= new BufferedReader(inStream);
buffWriter=new BufferedWriter(outStream);
String temp=null; // temp=stringa temporanea-una volta carica il nomr, poi cognome ecc
String nome=null;
String cognome=null;
String data=null;
int count =1; // per contare che riga sto leggendo
while((temp=buffRead.readLine())!= null){
if((count %3)==1)
nome =temp;

else if((count %3)==2)
cognome =temp;

else{
data =temp;
buffWriter.write(nome+";"+cognome+";"+data+"\r\n");
}
count++;
}
System.out.println("Copia effettuata,lette"+(count-1)+"righe");
}catch(FileNotFoundException fnfe){
System.out.println("Errore nelle crazioni dei flussi di input e/o output di byte da/verso i file");
System.out.println(fnfe.getMessage());
}catch(UnsupportedEncodingException uee){
System.out.println("Codifiche dei flussi di input e/o di output di caratteri errate");
System.out.println(uee.getMessage());
}catch(IOException ioe){
System.out.println("Errore in fase di lettura e/o scrittura di caratteri");
System.out.println(ioe.getMessage());
}
finally{
if(buffWriter!=null)
buffWriter.close();
if(buffRead!=null)
buffRead.close();
if(outStream!=null)
outStream.close();
if(buffWriter!=null)
buffWriter.close();
if(buffWriter!=null)
buffWriter.close();
if(buffWriter!=null)
buffWriter.close();
}

}

}

Chat Cliente-Server

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
BufferedReader lettore;
Socket sock;
PrintWriter scrittore;
JTextField t1=new JTextField(20);
JTextArea a1=new JTextArea(20,50);
JScrollPane scrollPane;
public void finestra(){
JFrame f1=new JFrame("semplice chat");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1=new JPanel();
JButton b1=new JButton("invia il messaggio");
p1.setLayout(new GridLayout(3,1));
scrollPane =new JScrollPane(a1);
p1.add(scrollPane,BorderLayout.CENTER);
p1.add(t1);
p1.add(b1);
b1.addActionListener(new InviaMessaggio());
f1.add(p1);
f1.setSize(500,500);
f1.setVisible(true);
Thread tt=new Thread(new InArrivoDalServer());
creaCollegamento();
tt.start();
}
public class InviaMessaggio implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
scrittore.write(t1.getText());
scrittore.println(t1.getText());
//per riazzerare il metodo
scrittore.flush();
}catch(Exception ex){ex.printStackTrace();}
t1.setText("");
}
}
public void creaCollegamento(){
try{
sock=new Socket("192.168.3.18",4242);
lettore= new BufferedReader(new InputStreamReader(sock.getInputStream()));
scrittore= new PrintWriter(sock.getOutputStream());
System.out.println("collegamento stabilito");
}catch(Exception ex){
ex.printStackTrace();
}
}
public class InArrivoDalServer implements Runnable{
public void run(){
String messaggio;
try{
while((messaggio=lettore.readLine())!= null){
a1.setText(messaggio);
System.out.println("leggo il messaggio:"+messaggio);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String[]args){
Client cc=new Client();
cc.finestra();
}
}



import java.io.*;
import java.net.*;
import java.util.*;
public class ServerSemplice {
PrintWriter[] clienti=new PrintWriter[20];
public class ElaboraCliente implements Runnable{
BufferedReader lettore=null;
Socket sock=null;
//creo lo Stream di input
public ElaboraCliente(Socket c){
try{
sock=c;
lettore=new BufferedReader(new InputStreamReader(sock.getInputStream()));
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override public void run(){
String messaggio;
try{
while((messaggio=lettore.readLine())!=null){
System.out.println("ho letto: "+messaggio);
InetAddress indirizzo=sock.getInetAddress();
String ind=indirizzo.toString();
String m=ind+" "+messaggio;
chiacchierone(messaggio);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public void go(){
try{
//portone di ascolto
ServerSocket portone=new ServerSocket(4242);
int i=0;
while(true){
//creo il collegamento con il cliente che ha bussato al portone
Socket portina=portone.accept();
//creo lo stream per scrivere i messaggi al cliente
PrintWriter scrittore=new PrintWriter(portina.getOutputStream());
//salvo in un array questo stream di output
clienti[i]=scrittore;
i++;
//........
//creo uno stream di input per quel cliente e mi metto a ricevere i suoi messaggi
//spedisco il messaggio ricevuto a tutti i client
Thread t=new Thread(new ElaboraCliente(portina));
t.start();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public void chiacchierone(String messaggio){
for(int i=0;i<20;i++){
if(clienti[i]!=null){
try{
clienti[i].println(messaggio);
clienti[i].flush();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
public static void main(String args[]){
ServerSemplice s=new ServerSemplice();
s.go();
}
}