Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [JAVA] ObjectOutputStream.writeBoolean(false)

    Ciao a tutti ragazzi avrei questo problema , so che esiste il DataInput ed Output Per i tipi primitivi , ma per comodità ho scoperto che il ObjectOutput ed Input Stream Ha i metodi per leggere e scrivere boolean. Quindi ho detto perchè aprirmi un altro stream se posso fare tutto in uno ! E allora ecco che mi fa questo problema:

    Ragazzi scrivo un boolean con writeBoolean(false);
    Ma il Server nonostante la .accept() vada a buon fine , appena faccio ObjectInputStream.readBoolean(); sempre lato server ! NON RICEVE NULLAAAA
    Non me lo legge ! Come è possibile ?!?

    Scusate il disturbo , grazie sullla fiducia =).

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [JAVA] ObjectOutputStream.writeBoolean(false)

    Originariamente inviato da AlienLearn
    Ciao a tutti ragazzi avrei questo problema , so che esiste il DataInput ed Output Per i tipi primitivi , ma per comodità ho scoperto che il ObjectOutput ed Input Stream Ha i metodi per leggere e scrivere boolean.
    Sia DataXXXStream (XXX sta per Input o Output) che ObjectXXXStream implementano la interfaccia DataXXX (quindi entrambi trattano I/O di primitivi). Ma gli ObjectXXXStream hanno in più la capacità di de/serializzare gli oggetti. Se questo non ti serve, è inutile usarli.

    Originariamente inviato da AlienLearn
    Ragazzi scrivo un boolean con writeBoolean(false);
    Ma il Server nonostante la .accept() vada a buon fine , appena faccio ObjectInputStream.readBoolean(); sempre lato server ! NON RICEVE NULLAAAA
    Lo fai un flush() dopo la scrittura?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Si l'ho fatto subito dopo il writeBoolean(), per questo non capisco come possa non funzionare , ti giuro sto impazzendo :-|...

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da AlienLearn
    Si l'ho fatto subito dopo il writeBoolean(), per questo non capisco come possa non funzionare
    Bisognerebbe vedere il codice, senza potrei solo tirare ad indovinare.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Allora l'esercizio è questo:
    http://imageshack.us/f/267/catturalg.jpg/

    Il Codice è questo:
    codice:
    //Call Msg1.java
    import java.io.*;
    
    public class CallMsg1 implements Serializable{
    	int x;
    	int y;
    	
    	CallMsg1 (int a,int b) {
    		x=a;
    		y=b;
    	}
    	
    	int getX () {
    		return x;
    	}
    	
    	int getY () {
    		return y;
    	}
    }
    codice:
    //Server.java
    import java.net.*;
    import java.io.*;
    
    public class Server implements Runnable{
    	Potenza pot;
    	ProdottoMatrici prodMatrix;
    	
    	ObjectInputStream OIS;
    	ObjectOutputStream OOS;
    	
    	public Server (int id,Socket s) throws IOException{
    		//Apro Gli Stream Di Comunicazione 
    		OIS = new ObjectInputStream (s.getInputStream());
    		OOS = new ObjectOutputStream (s.getOutputStream());
    		//Poi Li DEVO chiudere Nel THREAD
    		new Thread(this,"Thread n°" + id).start(); //Facccio Partire Il Thread Server Ad Ogni Accept() Andato a Buon Fine
    									   //Quindi poi Un thread Singolo Si OCcuperà si una comunicazioni Col Client
    									   // Se ci sarà un altro client , della comunicazione se ne occupeerò un altro Thread 
    									   //Del Server
    		
    	}
    	
    	public static void main (String args []) throws IOException{
    		ServerSocket ss = new ServerSocket (22222); //Creo la Serv.Socket
    		int idServ=0;
    		Socket socket;
    		
    		while (true) {
    			socket = ss.accept();
    			System.out.println("Richiesta Accettata");
    			idServ++;
    			
    			new Server(idServ,socket);
    			socket.close();
    		}
    		
    	}
    	public void run () {
    		try {
    			System.out.println("Thread partito DENTRO RUN");
    			boolean scelta = (boolean) OIS.readBoolean(); //Se scelta = true allora primo servizio , potenza 
    									   //Se scelta = false allora secondo servizio , Matrici
    			System.out.println("Boolean Della Scelta Fatta Dal Client Letta ");
    			if (scelta) { //Ossia Se è true Quindi Il primo Servizio 
    				/*Faccio Il Primo Servizio*/
    				System.out.println("Sono Dentro l'if Quindi Il writeBoolean Ha funzionato");
    				CallMsg1 msg1 = (CallMsg1)OIS.readObject();	//Leggo Il CallMsg1
    				System.out.println("Oggetto Ricevuto Dal Server");
    				Potenza pot = new Potenza () ; //Oggetto Potenza 
    				double result;
    				result = pot.pow2int(msg1.getX(),msg1.getY());
    				/*Ora che ho prodotto Il Risultato Lo Devo Inviare Al client*/
    				OOS.writeDouble (result); // Lo Ritorno Sullo Stream Di OutputCome Double
    				
    			}
    			else { //Ossia se writeBoolean è false e quindi sta richiedento il servizio 2
    				
    				ProdottoMatrici pm = (ProdottoMatrici)OIS.readObject();
    				System.out.println("Oggetto Ricevuto Dal Server");
    				int C [] [] = pm.getProdotto();
    				/*Adesso L'oggetto conterrà la matrice C Quindi Il risultato Del Prodotto*/
    				/*Devo inviare Al Client l'oggetto e il risultato per poi Lui Lo deve Visualizzare*/
    				OOS.writeObject(pm);
    				
    			}
    			OIS.close(); // Chiudo Gli Stream
    			OOS.close(); //Chiudo Gli Stream
    			
    		}
    		catch (IOException ioe) {}
    		catch (ClassNotFoundException cnfe) {}
    	}
    }
    codice:
     //Client.java
    import java.net.*;
    import java.io.*;
    
    public class Client implements Serializable{
    	static BufferedReader BR;
    	
    	public static void main (String args []) throws IOException, ClassNotFoundException, InterruptedException{
    		Socket socket = new Socket ("localhost",22222);
    		System.out.println("Connessione Eseguita Con Il Server");
    		//Selezione Dei Servizi Sul Server
    		//BufferedReader BR;
    		boolean continua =true;
    		//while (continua) {
    			BR = new BufferedReader (new InputStreamReader(System.in) );
    			System.out.println("1) Dati 2 valori x e y , Ritornami Elevazione A Potenza: ");
    			System.out.println("2) Date 2 Matrici Quadrate Ritornami Il Prodotto:  ");
    			System.out.println("3) Esci");
    			System.out.println("--> Inserisci La tua Scelta : ");
    			int scelta = Integer.parseInt(BR.readLine());
    			
    			Client client = new Client ();
    			switch (scelta) {
    				case 1:
    					client.CallServizioPotenza(socket) ;
    					break;
    				case 2:
    					client.CallServizioMatrice(socket);
    					break;
    				case 3:
    					break;
    			}
    		//}
    		socket.close(); //Chiudo Il Socket 
    	}
    	
    	public void CallServizioPotenza (Socket s) throws IOException{
    		
    		BR = new BufferedReader (new InputStreamReader(System.in));
    		System.out.println("Inserisci Il Primo Valore: ");
    		int a= Integer.parseInt(BR.readLine());
    		System.out.println("Inserisci Il Secondo Valore: ");
    		int b= Integer.parseInt(BR.readLine());
    		CallMsg1 msg1 = new CallMsg1 (a,b);
    		
    		//Apro Lo Stream Sul Socket per scrivere Oggetto E anche per Scrivere Il Boolean true Per dirgli che è Il PRIMO SERVIZIO
    		ObjectOutputStream OOS = new ObjectOutputStream (s.getOutputStream());
    		System.out.println("Flusso Object Stream Creato");
    		OOS.writeBoolean(true); //Che per me significa , Server Vedi Che voglio Che Svogli Il Servizio 1 Ossia La potenza
    		System.out.println("Boolean Inviato Al Server Per Dirgli Che Servizio Deve Svolgere");
    		OOS.writeObject (msg1); //Scrivo L'oggetto Sullo Stream 
    		System.out.println("Oggetto Inviato");
    		OOS.close(); //CHIUDO LO STREAM OOS
    		
    		//Apro Lo Stream Sul Socket Per Riceve La risposta Dal Server
    		ObjectInputStream OIS = new ObjectInputStream (s.getInputStream());
    		double result;
    		result = OIS.readDouble(); //Leggo il Double Inviatomi Dal Server
    		System.out.println("Il Risultato è: " + result );
    		OIS.close();//Chiudo Lo Stream OIS
    		
    	}
    	
    	public void CallServizioMatrice (Socket s) throws IOException, ClassNotFoundException, InterruptedException {
    		System.out.println("Inserisci La dimensione Delle Matrici: ");
    		int d =Integer.parseInt(BR.readLine());
    		ProdottoMatrici prodM = new ProdottoMatrici (d); // Ho creato Le 3 matrici di Dimensione d , le tre Matrici A , B e C
    		prodM.insertMatrix (BR); // ho inserito Le 2 Matrici A e B
    		//Ora le devo Inviare Al Server Per L'elaborazione
    		
    		//Apro Lo Stream Sul Socket per scrivere Oggetto E anche per Scrivere Il Boolean true Per dirgli che è Il PRIMO SERVIZIO
    		ObjectOutputStream OOS = new ObjectOutputStream (s.getOutputStream());
    		System.out.println("Flusso Object Output Stream Creato");
    		boolean bool = false;
    		OOS.writeBoolean(bool); //Che per me significa , Server Vedi Che voglio Che Svogli Il Servizio 2 Ossia Prodotto Fra Matrici
    		OOS.flush();
    		System.out.println("Boolean Inviato Al Server Per Dirgli Che Servizio Deve Svolgere");
    		
    		Thread.sleep(3000);
    		//OOS.writeObject (prodM); //Scrivo L'oggetto Sullo Stream 
    		System.out.println("Oggetto Inviato");
    		//OOS.close(); //CHIUDO LO STREAM OOS
    		
    		//Apro Lo Stream Sul Socket Per Riceve La risposta Dal Server
    		ObjectInputStream OIS = new ObjectInputStream (s.getInputStream());
    		int C [] [] = new int [d] [d];
    		prodM = (ProdottoMatrici)OIS.readObject(); //Leggo il Double Inviatomi Dal Server
    		/*Ora che ho letto l'ggetto Devo Visualizzare La Matrice Risultato */
    		C = prodM.getC(); /*Prendo La Matrice Risultato Elaborata Dal Server*/
    		/*Ora La visualizzo*/
    		prodM.printMatrix (C);
    		OIS.close();//Chiudo Lo Stream OIS
    		
    	}
    	
    }
    codice:
    //Potenza.java
    import java.net.*;
    import java.io.*;
    
    public class Potenza {
    	int x,y;
    	double result;
    	
    	public Potenza () {
    		x=0;
    		y=0;
    		result=0;
    	}
    	
    	public int getX () {
    		return x;
    	}
    	
    	public int getY () {
    		return y;
    	}
    	
    	public double getResult() {
    		return result;
    	}
    	
    	public double pow2int (int x,int y) {
    		return Math.pow(x,y);
    	}
    }
    codice:
    //ProdottoMatrici.java
    import java.net.*;
    import java.io.*;
    
    public class ProdottoMatrici implements Serializable{
    	int A [] []; //Matrice Ingresso 1
    	int B [] []; //Matrice Ingresso 2
    	int C [] []; //Matrice Uscita
    	int dim; //Dimensione delle Matrici Quadrate
    	
    	public ProdottoMatrici (int d) {
    		
    		dim=d; //Inizializzo La dimensione Della Classe ProdottoMatrici
    		A = new int [d] [d];
    		B = new int [d] [d];
    		C = new int [d] [d];
    		
    		//Ho creato Le matrici Ingresso E Uscita
    	}
    	
    	int [] [] getA () {
    		return A;
    	}
    	
    	int [] [] getB () {
    		return B;
    	}
    	
    	int [] [] getC () {
    		return C;
    	}
    	
    	void insertMatrix (BufferedReader BR) throws IOException{
    		System.out.println("Inserimento Matrice: A");
    		for (int i=1; i<=dim; i++) {
    			for (int y=1; y<=dim;y++) {
    				System.out.println("Inserisci Elemento Posto: [" +i+","+y+"]");
    				A [i-1] [y-1] = Integer.parseInt(BR.readLine());
    			}
    		}
    		
    		System.out.println("Inserimento Matrice: B");
    		
    		for (int i=1; i<=dim; i++) {
    			for (int y=1; y<=dim;y++) {
    				System.out.println("Inserisci Elemento Posto: [" +i+","+y+"]");
    				B [i-1] [y-1] = Integer.parseInt(BR.readLine());
    			}
    		}
    		
    		System.out.println("Ho finito Inserimento Delle Matrici: ");
    		
    		
    		
    	}
    	
    	int getProdRiga (int M [] [], int numRiga) { 
    		numRiga= numRiga -1; //Io gli passerò riga 1 e lui elaborerà la riga 0 del vettore
    		int temp=1;
    		for (int i=0;i<dim;i++) { //Col primo for scorro tutta una riga
    			temp = M [numRiga] [i] * temp;
    		}
    		return temp;
    	}
    	
    	int getProdColonna (int M [] [], int numColonna) {
    		numColonna = numColonna -1; //Io gli passerò colonna 1 e lui elaborerà la colonna 0 del Vettore
    		int temp=1;
    		for (int i=0;i<dim;i++) { //Col primo for scorro tutta una riga
    			temp = M [i] [numColonna] * temp;
    		}
    		return temp;
    	}
    	int [] [] getProdotto() {
    		for (int i=1; i<=dim; i++) {
    			for (int y=1; y<=dim;y++) {
    				C [i-1] [y-1] = (getProdRiga (A,i)) * (getProdColonna(B,y));
    			}
    		}
    	return C;
    	}
    	
    	void printMatrix (int [] [] V) {
    		for (int i=1; i<=dim; i++) {
    			for (int y=1; y<=dim;y++) {
    				System.out.println("Elemento Posto: [" +i+","+y+"] = (" +V[i-1] [y-1]+")" );
    				
    			}
    		}
    		
    	}
    }

    In pratica: Quando scelgo il servizio 2 , con il writeboolean invio dal client al server un boolean per fargli capire che deve fornirmi il secondo servizio però lui il server non riceve assolutamente nulla , tanto che il printf subito dopo IL OIS.readBoolean(); Non Appare

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.