v1sc3rr4k
29-02-2012, 17:33
Salve a tutti :D
Dovevo fare un programma che da client tramite server fa il prodotto scalare tra matrici quadrate, con la condizione che per ogni riga della prima il prodotto con l'intera seconda matrice venisse fatto da un server e così via. Quindi per matrici NxN ho bisogno di N server! Fin qui tutto bene. Il programma lavora e i calcoli sono corretti il problema è che non so come far attendere al Client che tutti i Threads lanciati abbiamo finito di elaborare i calcoli. Qualcuno ha qualche dritta da darmi sulla sincronizzazione oppure può consigliarmi sul codice. Ogni consiglio è ben accetto :)
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
public class Client extends Thread
{
public static void main(String args[]) throws IOException
{
Socket mainSocket = new Socket("localhost" , 1500);
DataOutputStream out = new DataOutputStream(mainSocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(mainSocket.getInputStream()));
Scanner s = new Scanner(System.in);
int size, port;
float multiplying[][][] , result[][];
System.out.println("Dammi la dimensione delle matrici quadrate: ");
size = s.nextInt();
result = new float[size][size];
multiplying = new float[2][size][size];
System.out.println();
for(int index = 1 ; index < 3; index++)
{
System.out.println("Inserimento della matrice " + index + " !" );
System.out.println("__________________________________________" );
for(int j = 0 ; j < size ; j++)
for(int k = 0 ; k < size ; k++)
{
System.out.println("Dammi il prossimo elemento: ");
multiplying[j][k] = s.nextFloat();
}
}
out.write(size);
// ricevo dal mainServer le porte dagli altri server chiamo l'oggetto client handler che avviera la connessione
for(int z = 1 ; z <= size ; z++)
{
System.out.println("Ricezione server " + z + " !");
port = in.read();
ClientHandler c = new ClientHandler(port , multiplying , result , size);
c.start();
}
}
}
L'altra classe:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler extends Thread
{
public ClientHandler(int port, float matrix[][][], float result[][], int size) throws IOException
{
this.socketN = new Socket("localhost" , port);
out = new DataOutputStream ( this.socketN.getOutputStream());
in = new DataInputStream ( this.socketN.getInputStream() );
this.matrix = matrix;
this.result = result;
this.size = size;
}
public void run()
{
try
{
int row = in.read();
out.write(size);
for(int i = 0 ; i < size; i++)
out.writeFloat(matrix[0][row][i]);
for(int i = 0; i < size ; i++)
for(int j = 0 ; j < size ; j++ )
out.writeFloat(matrix[1][i][j]);
for(int i = 0 ; i< size ; i++)
result[row][i] = in.readFloat();
[I]//IL CLIENT DOVREBBE ASPETTARE CHE ARRIVO QUI
return;
}
catch(Exception e)
{
e.printStackTrace();
}
}
private Socket socketN;
private DataOutputStream out;
private DataInputStream in;
private float matrix[][][];
private float result[][];
private int size;
}
Dovevo fare un programma che da client tramite server fa il prodotto scalare tra matrici quadrate, con la condizione che per ogni riga della prima il prodotto con l'intera seconda matrice venisse fatto da un server e così via. Quindi per matrici NxN ho bisogno di N server! Fin qui tutto bene. Il programma lavora e i calcoli sono corretti il problema è che non so come far attendere al Client che tutti i Threads lanciati abbiamo finito di elaborare i calcoli. Qualcuno ha qualche dritta da darmi sulla sincronizzazione oppure può consigliarmi sul codice. Ogni consiglio è ben accetto :)
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
public class Client extends Thread
{
public static void main(String args[]) throws IOException
{
Socket mainSocket = new Socket("localhost" , 1500);
DataOutputStream out = new DataOutputStream(mainSocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(mainSocket.getInputStream()));
Scanner s = new Scanner(System.in);
int size, port;
float multiplying[][][] , result[][];
System.out.println("Dammi la dimensione delle matrici quadrate: ");
size = s.nextInt();
result = new float[size][size];
multiplying = new float[2][size][size];
System.out.println();
for(int index = 1 ; index < 3; index++)
{
System.out.println("Inserimento della matrice " + index + " !" );
System.out.println("__________________________________________" );
for(int j = 0 ; j < size ; j++)
for(int k = 0 ; k < size ; k++)
{
System.out.println("Dammi il prossimo elemento: ");
multiplying[j][k] = s.nextFloat();
}
}
out.write(size);
// ricevo dal mainServer le porte dagli altri server chiamo l'oggetto client handler che avviera la connessione
for(int z = 1 ; z <= size ; z++)
{
System.out.println("Ricezione server " + z + " !");
port = in.read();
ClientHandler c = new ClientHandler(port , multiplying , result , size);
c.start();
}
}
}
L'altra classe:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler extends Thread
{
public ClientHandler(int port, float matrix[][][], float result[][], int size) throws IOException
{
this.socketN = new Socket("localhost" , port);
out = new DataOutputStream ( this.socketN.getOutputStream());
in = new DataInputStream ( this.socketN.getInputStream() );
this.matrix = matrix;
this.result = result;
this.size = size;
}
public void run()
{
try
{
int row = in.read();
out.write(size);
for(int i = 0 ; i < size; i++)
out.writeFloat(matrix[0][row][i]);
for(int i = 0; i < size ; i++)
for(int j = 0 ; j < size ; j++ )
out.writeFloat(matrix[1][i][j]);
for(int i = 0 ; i< size ; i++)
result[row][i] = in.readFloat();
[I]//IL CLIENT DOVREBBE ASPETTARE CHE ARRIVO QUI
return;
}
catch(Exception e)
{
e.printStackTrace();
}
}
private Socket socketN;
private DataOutputStream out;
private DataInputStream in;
private float matrix[][][];
private float result[][];
private int size;
}