Premetto che questo è il mio primo programma in Java quindi ho parecchie lacune.
Devo realizzare un sistema client server, dove il server può ricevere infinite connessioni e crea un thread per gestire ogni connessione.
Il server è CollabServer, il Client è ServizioCinema e CollabHandler è la classe che si occupa di gestire una singola connessione.
Ora ho numerosi problemi che principalmente sono:
Invio e ricezione di oggetti (o meglio vettori di oggetti);
Visibilità di variabili tra classi (non riesco a far vedere la stessa variabile a 2 classi differenti).
Il server parte, accetta le connessioni, ma arrivato alle dichiarazioni degli object input stream si blocca e inzia a dare problemi.
Stessa cosa per il client che si connette al server ma, una volta dichiarati gli object stream, cessa di funzionare.
posto il codice delle 3 classi sopra citate mettendo in grassetto i punti in cui da problemi, spero che qualcuno di voi mi possa aiutare.
CollabServer
codice:
package serverclientprova;
import java.net.*;
import java.io.*;
import java.util.*;
public class CollabServer {
//--------------------------------------------------
protected static FilmInProg [] vFiP; /*HO PROVATO A DICHIARARE
STATICA QUESTO VETTORE DI OGGETTI FilmInProg PER RENDERLA
VISIBILE A COLLABHANDLER MA NON FUNZIONA*/
protected String info = "";
protected BufferedReader bis;
protected BufferedWriter bos;
protected int nM;
public CollabServer() throws IOException {
MemorizzaFile();
nM = ContaMatrici();
vFiP = new FilmInProg [nM];
CreaVettore();
ServerSocket server = new ServerSocket(1414);
System.out.println ("Accepting connections...");
while(true) {
Socket client = server.accept(); //ogni richiesta di connessione viene accettata
System.out.println ("Accepted from " + client.getInetAddress());
try{
new CollabHandler(client, info, nM);
}catch(IOException e){
System.out.println("IO Exception" + "\n");
}//Istanziamo un oggetto CollabHandler per gestire la connessione }
}
}
public void MemorizzaFile () throws IOException {
try{
bis = new BufferedReader(new InputStreamReader(new FileInputStream("DbCinema.txt"))); //apriamo il file di testo contenente le info
String temp = "";
while( (temp = bis.readLine()) != null)
info = info + temp + "\n";
} catch(IOException e){
System.out.println("IO Exception" + "\n");
}
finally {
try {
bis.close();
} catch (IOException e) {
System.out.println("IO Exception" + "\n");
}
}
}
public int ContaMatrici () throws IOException { //conta il numero delle matrici che dovranno esser create
String inputLine = "";
int num = 0;
try {
bis = new BufferedReader(new InputStreamReader(new FileInputStream("DbCinema.txt"))); //apriamo il file di testo contenente le info
inputLine = bis.readLine();
while (inputLine.compareTo("FINE") != 0) {
bis.readLine();
inputLine = bis.readLine();
while (inputLine.compareTo("---") != 0) {
num++;
inputLine = bis.readLine();
}
inputLine = bis.readLine();
}
}
catch (IOException e) {
System.out.println("IO Exception" + "\n");
} finally {
try {
bis.close();
} catch (IOException e) {
System.out.println("IO Exception" + "\n");
}
}
return num;
}
public void CreaVettore () throws IOException {
String NomeF, NomeS, t;
int i = 0;
try{
bis = new BufferedReader(new InputStreamReader(new FileInputStream("DbCinema.txt")));
NomeF = bis.readLine();
while (NomeF.compareTo("FINE") != 0){
NomeS = bis.readLine();
t = bis.readLine();
while (t.compareTo("---") != 0){
vFiP[i] = new FilmInProg( NomeF, NomeS, t);
i++;
t = bis.readLine();
}
//per me questa cosa va gestita meglio... che facciamo continuiamo il while ugualemte????
NomeF = bis.readLine();
}
}catch (IOException e) {
System.out.println("IO Exception" + "\n");
}finally{
try {
bis.close();
} catch (IOException e) {
System.out.println("IO Exception" + "\n");
}
}
}
//--------------------------------------------------
public static void main(String args[]) throws IOException {
new CollabServer();
}
}