salve a tutti.. spero fortemente in un vostro aiuto perchè sono bloccato su questo problema da un po di tempo.
vi spiego: ho una classe server che appunto funziona da server, e che salva in una lista tutti i client che si connettono. quando tra i client, si connette uno con il nome di amministratore, il server gestisce una comunicazione a parte con lui. questo client amministratore ha un'interfaccia grafica dalla quale l'utente può connettersi al server e può eseguire alcune operazioni. per ora ho implementato solo una stampa di tutti i client connessi.
l'applicazione gira bene finche il client amministratore si connette, esegue la stampa dei client connessi e poi però non però perchè chiude la connessione, quando invece io vorrei che la connessione rimanga aperta per altre operazioni che l'utente vuole fare.
vi allego i vari sorgenti sperando che possiate aiutarmi! grazie
codice:package server; import java.io.*; import java.net.*; import java.util.Iterator; import utilities.ConstantType; import utilities.ListaClient; import utilities.Struttura; class GestoreClient extends Thread{ ListaClient list ; Socket s; String user; public GestoreClient(ListaClient list, Socket s,String u){ this.list = list; this.s = s; this.user=u; } public void run() { System.out.println(" Aggiungo il client alla lista..."); Struttura client = new Struttura(user); //setto ip,port e la socket System.out.println(" Setto i valori della porta,ip e socket del client connesso...."); client.setIp(s.getInetAddress().getHostAddress()); client.setPort(s.getPort()); client.setSocket(s); //popolo la struttura System.out.println(" Popolo la lista..."); boolean b=list.add(client); System.out.println(" CLIENT INSERITO NELLA STRUTTURA : " + b); System.out.println(""); System.out.println(" CLIENT CONNESSI : "+list.contaClient()); System.out.println(""); System.out.println("Ultimo client connesso: "); System.out.println("User: "+user); System.out.println("Port: "+client.getPort()); System.out.println("IP: "+client.getIp()); System.out.println(""); System.out.println("STAMPA LISTA"); list.Stampa(); } } class GestoreAdmin extends Thread{ private Socket socket;; private ListaClient list; public GestoreAdmin(Socket s,ListaClient l) { this.socket=s; this.list=l; } public void run(){ BufferedReader in; PrintWriter out; try { in=new BufferedReader(new InputStreamReader(socket.getInputStream())); out=new PrintWriter(socket.getOutputStream()); while(true){ System.out.println("Sto nel thread del client_admin..."); String msg=null; System.out.println(" Attendo messaggio dal Client_Admin..."); System.out.println(" connessione chiusa? "+socket.isClosed()); while((msg=in.readLine())!=null){ System.out.println(" connessione chiusa? "+socket.isClosed()); System.out.println("entro nel while"); if(msg.equals("esegui")){ System.out.println(" connessione chiusa? "+socket.isClosed()); System.out.println(" Ho ricevuto "+msg+"..."); System.out.println(" Preparo la stampa dei client connessi al server..."); Iterator<Struttura> it = list.Iterator(); while(it.hasNext()){ out.println(it.next().getUser()); out.flush(); } System.out.println(" connessione chiusa? "+socket.isClosed()); System.out.println(" Ho effettuato la stampa dei client connessi al server..."); //out.close(); System.out.println(" connessione chiusa? "+socket.isClosed()); } if(msg.equals("refresh")){ System.out.println(" Effettuo il refresh.."); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(ConstantType.PORT); System.out.println(" SERVER LANCIATO"); ListaClient list = new ListaClient(); //il server aspetta sempre una connessione.Quando un client si connette,viene lanciato un //Thread a cui fornisco la socket e la lista dei Client gia connessi while(true){ System.out.println(" Attendo la connessione di qualche utente"); Socket s = ss.accept(); System.out.println(" Ho eseguito la accept"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //PrintWriter out=new PrintWriter(s.getOutputStream()); System.out.println(" Ho aperto un buffer in in e out"); System.out.println(" Leggo l'input"); String user = br.readLine(); if(user.equals("administrator")){ GestoreAdmin ga=new GestoreAdmin(s,list); ga.start(); }else{ GestoreClient g = new GestoreClient(list,s,user); g.start(); } System.out.println(" ho finito il main..."); } } }questi sono i metodi incriminati.. help please!codice:public class provaPresentazioneClientAdmin extends JFrame { private static final long serialVersionUID = 1L; private JPanel jPanel = null; private JLabel jLabel = null; private JButton enter = null; private JProgressBar jProgressBar = null; private JLabel jLabel1 = null; private JPanel jPanel1 = null; // @jve:decl-index=0:visual-constraint="676,53" private JLabel jLabel2 = null; private JButton esegui = null; private JPanel jPanel2 = null; private JButton refresh = null; private JLabel listlabel = null; private JList listclient = null; private Socket clientAdmin; private BufferedReader in=null; private PrintWriter out=null; public provaPresentazioneClientAdmin() { super(); initialize(); } private void initialize() { this.setSize(657, 505); this.setResizable(false); this.setContentPane(getJPanel()); this.setTitle("Client_Administrator"); } private void initialize1() { this.setSize(657, 505); this.setContentPane(getJPanel1()); this.setTitle("seconda finestra"); } private JPanel getJPanel() { if (jPanel == null) { jLabel1 = new JLabel(); jLabel1.setBounds(new Rectangle(0, 5, 649, 47)); jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jLabel1.setHorizontalTextPosition(SwingConstants.CENTER); jLabel1.setFont(new Font("Edwardian Script ITC", Font.ITALIC, 36)); jLabel1.setText("Welcome to Client Administrator"); jLabel = new JLabel(new ImageIcon("C:\\Documents and Settings\\Matteo\\Documenti\\Downloads\\icona_pc.jpeg")); jLabel.setBounds(new Rectangle(53, 60, 558, 319)); //jLabel.setText("JLabel"); jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED); jLabel.setIcon(new ImageIcon("C:/Documents and Settings/Matteo/Documenti/Immagini/icona_pc.jpg")); jPanel = new JPanel(); jPanel.setLayout(null); jPanel.add(jLabel, null); jPanel.add(getEnter(), null); jPanel.add(getJProgressBar(), null); jPanel.add(jLabel1, null); } return jPanel; } private JButton getEnter() { if (enter == null) { enter = new JButton(); enter.setBounds(new Rectangle(270, 383, 129, 37)); enter.setText("Enter"); enter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed() try { clientAdmin=new Socket("localhost",8080); in=new BufferedReader(new InputStreamReader(clientAdmin.getInputStream())); out=new PrintWriter(clientAdmin.getOutputStream()); out.println("administrator"); out.flush(); System.out.println("inizia l'amministratore"); initialize1(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } return enter; } private JProgressBar getJProgressBar() { if (jProgressBar == null) { jProgressBar = new JProgressBar(); jProgressBar.setBounds(new Rectangle(201, 428, 249, 14)); } return jProgressBar; } private JPanel getJPanel1() { if (jPanel1 == null) { jLabel2 = new JLabel(); jLabel2.setBounds(new Rectangle(46, 27, 180, 31)); jLabel2.setText("Lista dei client connessi"); jPanel1 = new JPanel(); jPanel1.setLayout(null); jPanel1.setBounds(new Rectangle(0, 0, 657, 474)); jPanel1.add(jLabel2, null); jPanel1.add(getEsegui(), null); jPanel1.add(getJPanel2(), null); jPanel1.add(getRefresh(), null); } return jPanel1; } private JButton getEsegui() { if (esegui == null) { esegui = new JButton(); esegui.setBounds(new Rectangle(478, 26, 91, 30)); esegui.setText("Esegui"); esegui.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed() di esegui.."); // TODO Auto-generated Event stub actionPerformed() try { System.out.println("creo l'out"); out.println("esegui"); out.flush(); System.out.println("spedisco 'esegui'"); String name; DefaultListModel m=new DefaultListModel();//per mettere i dati da visualizzare nella Jlist listclient.setModel(m); System.out.println("attendo la risposta dal server"); while((name=in.readLine())!=null ){ System.out.println("name: "+name); m.addElement(name); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } return esegui; } private JPanel getJPanel2() { if (jPanel2 == null) { listlabel = new JLabel(); listlabel.setBounds(new Rectangle(1, 1, 371, 367)); jPanel2 = new JPanel(); jPanel2.setLayout(null); jPanel2.setBounds(new Rectangle(45, 84, 372, 368)); jPanel2.add(listlabel, null); //jPanel2.add(new JScrollPane(listclient)); jPanel2.add(getListclient(), new JScrollPane(listclient) ); listclient.addListSelectionListener(new ListenerListClient()); } return jPanel2; } private JButton getRefresh() { if (refresh == null) { refresh = new JButton(); refresh.setBounds(new Rectangle(479, 61, 90, 32)); refresh.setText("Refresh"); refresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed() di refresh"); // TODO Auto-generated Event stub actionPerformed() out.println("refresh"); out.flush(); } }); } return refresh; } private JList getListclient() { if (listclient == null) { listclient = new JList(); listclient.setBounds(new Rectangle(1, 1, 372, 368)); } return listclient; } private class ListenerListClient implements ListSelectionListener{ public void valueChanged(ListSelectionEvent e){ if(e.getValueIsAdjusting()) return; String v=(String)listclient.getSelectedValue(); if(v==null) return; } } } // @jve:decl-index=0:visual-constraint="3,45"

Rispondi quotando