Socket Leitstelle für Funkspiel

  • Themenstarter Themenstarter DeBukkIt
  • Beginndatum Beginndatum
D

DeBukkIt

Gast
Hallöle!

Vorgeschichte:
Ich bin dabei, eine kleine Leitstelle für ein Funkspiel zu programmieren. Das heißt, dass der Nutzer einen Client nutzt, der einen Funk-Status senden kann und Alarm-Meldungen empfangen kann. Dazu habe ich bereits fast alle grafischen Oberflächen gebaut.

Beschreibung des Problems:
Der Nutzer ist die Client-Seite der Socket-Geschichte, weshalb das Senden vom Status kein Problem ist, da er nur eine Verbindungs-Anfrage zu machen braucht und dann über den PrintWriter (Client) bzw. Scanner (Server) dann überträgt. Problem ist aber die andere Seite. Denn der Server kann keine Verbindung zum Client aufbauen, da diese doch immer von dessen Seite aus erfolgen muss (außer er hat einen Socket-Server und gibt Ports auf Router und Firewall frei -> Tod, wenn das jeder Mitspieler machen müsste!), müsste der Client entweder alle paar Sekunden kurz aufbauen und bei einer Alarmierung die Daten empfangen, andernfalls wieder abbrechen und es erneut versuchen, oder die Verbindung wird ununterbrochen gehalten (ist das möglich per Threadding?).

Weitere Schwierigkeiten:
Es wird bei der "Alle-paar-Sekunden-Anfragen"-Methode ständig eine Connection-Refused-Exception geben. Außerdem meldet der Server nach ein paar Sekunden die Exeption, dass der Client, der eigentlich am Anfang jeder Verbindung seine ID senden soll, nichts sendet und beendet den Lauf mit einer Exception im Thread "main".

Zusammenfassende Fragen:
Wie kann der Server beim Client eine Verbindung erfragen bzw. wie kann man mit mehreren Nutzern gleichzeitig in Verbindung bleiben, damit sie im Falle einer Alarmierung speziell für einen bestimmten Client die Daten empfangen kann?
Wie verhindere ich ständige Exceptions auf Seiten von Client und Server?
Kennt jemand ein vergleichbares Projekt?
Hätte Interesse einmal etwas derartiges auszuprobieren?

Vielen Dank im Voraus,
DeBukkIt
 
Projekt bis jetzt:
Server:
Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netbeansguitest;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leonard
 */
public class guiLeitstelle extends javax.swing.JFrame {

    /**
     * Creates new form guiLeitstelle
     */
    public guiLeitstelle() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jRadioButton7 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();
        jRadioButton9 = new javax.swing.JRadioButton();
        jRadioButton10 = new javax.swing.JRadioButton();
        jRadioButton11 = new javax.swing.JRadioButton();
        jRadioButton12 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Leitstelle - Funkspiel Steinfurt");

        jLabel1.setText("Funkspiel Steinfurt LEITSTELLE");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Feuerwehr", "HO, HO", "ELW", null},
                {"Feuerwehr", "HO, HO", "LF-20/16", null},
                {"Feuerwehr", "HO, HO", "LF-16 TS", null},
                {"Feuerwehr", "HO, HO", "GW-L", null},
                {"Feuerwehr", "HO, HO", "MTW", null},
                {"Feuerwehr", "HO, LE", "TLF-16/25", null},
                {"Feuerwehr", "HO, LE", "LF-8", null},
                {"Feuerwehr", "HO, LE", "MTW", null},
                {"Feuerwehr", "HO, LE", "LF-16 TS", null},
                {"Feuerwehr", "ST, BO", "DLK-23/12", null},
                {"Rettungsdienst", "ST, BO", "NEF", null},
                {"Rettungsdienst", "ST, BF", "RTW", null}
            },
            new String [] {
                "FW / RD", "FW + LZ", "Fahrzeug", "Funk-Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowHeight(23);
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Auswählen");
        jRadioButton1.setName("");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Auswählen");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Auswählen");

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText("Auswählen");

        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setText("Auswählen");

        buttonGroup1.add(jRadioButton6);
        jRadioButton6.setText("Auswählen");

        buttonGroup1.add(jRadioButton7);
        jRadioButton7.setText("Auswählen");

        buttonGroup1.add(jRadioButton8);
        jRadioButton8.setText("Auswählen");

        buttonGroup1.add(jRadioButton9);
        jRadioButton9.setText("Auswählen");

        buttonGroup1.add(jRadioButton10);
        jRadioButton10.setText("Auswählen");

        buttonGroup1.add(jRadioButton11);
        jRadioButton11.setText("Auswählen");

        buttonGroup1.add(jRadioButton12);
        jRadioButton12.setText("Auswählen");

        jLabel2.setText("Stichwort");

        jLabel3.setText("Ort");

        jLabel4.setText("Beschreibung");

        jButton1.setText("Alarmieren!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Sonderrechte");

        jLabel5.setText("©17.04.2012 Leonard Bienbeck");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 763, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton4)
                            .addComponent(jRadioButton5)
                            .addComponent(jRadioButton6)
                            .addComponent(jRadioButton7)
                            .addComponent(jRadioButton8)
                            .addComponent(jRadioButton9)
                            .addComponent(jRadioButton10)
                            .addComponent(jRadioButton11)
                            .addComponent(jRadioButton12)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(184, 184, 184)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(11, 11, 11))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextField2)
                                .addComponent(jTextField1)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton12))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel5)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(evt.getSource() == jButton1){
                       
            ServerSocket server = null;
            try {
                server = new ServerSocket(8112);
            } catch (IOException ex) { Logger.getLogger(guiLeitstelle.class.getName()).log(Level.SEVERE, null, ex); }
            
            
   
            while(true){
                
                Socket client = null;
                              
                try{
                    client = server.accept();
                    Scanner in = new Scanner(client.getInputStream());
                    PrintWriter out = new PrintWriter(client.getOutputStream(), true);
                    
                    //Alarm-Daten
                    String _stw = jTextField1.getText();
                    String _ort = jTextField2.getText();
                    String _bes = jTextField3.getText();
                    
                    
                                       
                    //RADIO BUTTIN No1 (ELW HO-HO)
                    if(jRadioButton1.isSelected()){
                        if(in.nextLine() == "jRadioButton1"){
                            
                            if(jCheckBox1.isSelected()){
                                new sendAlarmMessage(_stw, _ort, _bes, "Ja", in, out);
                            } else {
                                new sendAlarmMessage(_stw, _ort, _bes, "Nein", in, out);
                            }   
                            
                            break;
                   
                        } 
                    }
                    
                    //RADIO BUTTIN No2 (LF-20/16 HO-HO)
                    if(jRadioButton2.isSelected()){
                        if(in.nextLine() == "jRadioButton2"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No3 (LF-16 TS HO-HO)
                    if(jRadioButton3.isSelected()){
                        if(in.nextLine() == "jRadioButton3"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No4 (GW-L HO-HO)
                    if(jRadioButton4.isSelected()){
                        if(in.nextLine() == "jRadioButton4"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No5 (MTW HO-HO)
                    if(jRadioButton5.isSelected()){
                        if(in.nextLine() == "jRadioButton5"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No6 (TLF-16/25 HO-LE)
                    if(jRadioButton6.isSelected()){
                        if(in.nextLine() == "jRadioButton6"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No7 (LF-8 HO-LE)
                    if(jRadioButton7.isSelected()){
                        if(in.nextLine() == "jRadioButton7"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No8 (MTW HO-LE)
                    if(jRadioButton8.isSelected()){
                        if(in.nextLine() == "jRadioButton8"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No9 (LF-16 TS HO-LE)
                    if(jRadioButton9.isSelected()){
                        if(in.nextLine() == "jRadioButton9"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No10 (DLK-23/12 ST-BO)
                    if(jRadioButton10.isSelected()){
                        if(in.nextLine() == "jRadioButton10"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No11 (NEF ST-BO)
                    if(jRadioButton11.isSelected()){
                        if(in.nextLine() == "jRadioButton11"){
                        
                        
                        
                        
                        } 
                    }
                    
                    //RADIO BUTTIN No12 (RTW ST-BF)
                    if(jRadioButton12.isSelected()){
                        if(in.nextLine() == "jRadioButton12"){
                        
                        
                        
                        
                        } 
                    }
                    
                                        
                } catch (IOException e){ e.printStackTrace(); }
                finally { if(client != null) { try { client.close();} catch (IOException e){ e.printStackTrace();} } }
          break;
        }}
    }                                        

    /**
     * @param args the command line arguments
     */
   public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(guiLeitstelle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(guiLeitstelle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(guiLeitstelle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(guiLeitstelle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new guiLeitstelle().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton10;
    private javax.swing.JRadioButton jRadioButton11;
    private javax.swing.JRadioButton jRadioButton12;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JRadioButton jRadioButton9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration                   
}
 
sendAlarmMessage.java:
Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netbeansguitest;

import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author Leonard
 */
public class sendAlarmMessage {
    
    public sendAlarmMessage(String Stichwort, String Ort, String Beschreibung, String Sonderrechte, Scanner inputStream, PrintWriter outputStream){
        
        outputStream.println("Stw: " + Stichwort);
        outputStream.println("Ort: " + Ort);
        outputStream.println("// " + Beschreibung);
        outputStream.println("SoSi-Freigabe: " + Sonderrechte + "!");
        
    }
    
}

Client:
Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netbeansguitest;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leonard
 */
public class testClient {
    
    public static void main(String args[]){
    
    while(true){    
    Socket server = null;
        
    try {
       
        server = new Socket ("localhost", 8112);
        Scanner in = new Scanner(server.getInputStream());
        PrintWriter out = new PrintWriter(server.getOutputStream(), true);
        
        out.println("jButton1");
        
        String _stw = in.nextLine();
        String _ort = in.nextLine();
        String _bes = in.nextLine();
        String _son = in.nextLine();
        
        System.out.println(_stw + " UND " + _ort + " UND " + _bes + " UND " + _son);    
        
        
        
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        if(server != null){
            try { server.close(); } catch (IOException e) {}
        }//If
    }//finally
       
    }//While
    
    }//Constructor
}//Class
 
Problem ist aber die andere Seite. Denn der Server kann keine Verbindung zum Client aufbauen, da diese doch immer von dessen Seite aus erfolgen muss

Ist bei einer Server-Client-Architektur auch so direkt nicht vorgesehen. Ich würde die Verbindung einfach konstant im Hintergrund halten und bei "Alarmierungen" den Client benachrichtigen.

wie kann man mit mehreren Nutzern gleichzeitig in Verbindung bleiben

Für jeden Nutzer einen eigenen Thread erstellen, der vom Socket liest / bzw. zum Socket schreibt.

Wie verhindere ich ständige Exceptions auf Seiten von Client und Server?

Ohne Codebezüge recht schwer zu sagen. Poste doch mal bitte die Exceptions, wie sie ausgegeben werden.
 
Vielen, vielen Dank, ESCS!
Dafür, dass du so schnell geantwortet hast und dir überhaupt die Mühe gemacht hast, dich durch mein Thema zu lesen!

Code:
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1585)
	at netbeansguitest.testClient.main(testClient.java:34)

Erfolgt allerdings nur, wenn der Client vor dem Server startet.

Gruß,
DeBukkIt
 

Dazu die JavaDoc:

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Also scheint höchstwahrscheinlich beim Senden entweder kein line seperator oder in einer anderen Kodierung gesendet worden zu sein. Dazu die JavaDoc von PrintWriter.println():

The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

Könnte also durchaus sein dass die Klasse Scanner zwangsläufig einen bestimmen line seperator braucht. Probiers einfach mal mit nem BufferedReader & nem BufferedWriter aus, die sollten zueinander kompatibel sein.
 
@TO
ich würde dir erstmal grundlagen in punkto netzwerk-i/o in verbindung mit java empfehlen ...

das wohl einfachste was jeder der neu in diesem thema ist machen sollte : einen simplen multi-user chat ... was bei dir schon scheitern würde da du ja offenbar noch nichts von "Threads" gehört hast bzw wie man diese bei sowas richtig einsetzt ...

ergo : grundwissen über Threads und deren verwendung einholen ...
 
Danke nochmal!
Ja, ich sitze gerade in "Java ist auch eine Insel" und quäle mich durch alle 1.500 Seiten durch. Leider sagt mir das nicht alles und ich werde Google nutzen müssen.

Eine letzte bitte noch:
@ESCS: Könntest du mir ein kleines Code-Schnipselchen erstellen, wie ich Thread-mäßig eine Verbindung zu 12 Client halten kann?

Gruß,
DeBukkIt
 
Danke nochmal!
Ja, ich sitze gerade in "Java ist auch eine Insel" und quäle mich durch alle 1.500 Seiten durch. Leider sagt mir das nicht alles und ich werde Google nutzen müssen.

Eine letzte bitte noch:
@ESCS: Könntest du mir ein kleines Code-Schnipselchen erstellen, wie ich Thread-mäßig eine Verbindung zu 12 Client halten kann?

Gruß,
DeBukkIt

also ich denke deine bitte kannst du dir klemmen ... da du mit threads 1) theoretisch unendlich viele verbindungen handlen kannst ... und 2) Threads zu den grundlagen von java gehören ...


wie auch in nem anderen thread zu einem anderen user gesagt : wir sind hier NICHT dafür da dir grundlagen beizubringen oder deine arbeit zu übernehmen ...
wenn du wirklich java lernen willst fang mit den grundlagen an und lass erstmal die finger vom net-I/O ...
weil so wird das nichts ...
 

Neue Themen


Zurück
Oben