tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
1
ZUGRIFFE
390
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    ShikaeshiRaito ShikaeshiRaito ist offline Grünschnabel
    Registriert seit
    Dec 2010
    Beiträge
    1
    Hallo Welt!

    Ich habe folgendes Problem:
    Nachdem ich mich durch das Erstellen einer Java-Server- sowie Clientapplikation gekämpft habe, möchte er mir nicht mehr anzeigen als ein graues Fenster, ich weiß nicht mehr weiter...

    So sieht das ganze aus:

    http://www.abload.de/img/grauesfensterzgak.jpg


    Und das hier ist mein Quelltext:


    chatserver.java

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    
    import java.net.*;
    import java.io.*;
    import java.util.*;
     
    public class chatserver implements Runnable
    {
        public static final int PORT = 8765;
        protected ServerSocket listen;
        protected Vector connections;
        Thread connect;
     
        public chatserver()
        {
            try
            {
                listen = new ServerSocket(PORT);
            } catch (IOException e)
            {
                System.err.println("Fehler beim Erzeugen der Sockets:"+e);
                System.exit(1);
            }
     
            connections = new Vector();
     
            connect = new Thread(this);
            connect.start();
        }
     
        public void run()
        {
            try
            {
                while(true)
                {
                    Socket client=listen.accept();
     
                    connection c = new connection(this, client);
                    connections.addElement(c);
                }
            } catch (IOException e)
            {
                System.err.println("Fehler beim Warten auf Verbindungen:"+e);
                System.exit(1);
            }
        }
     
        public static void main(String[] args)
        {
            new chatserver();
        }
     
        public void broadcast(String msg)
        {
            int i;
            connection you;
     
            for (i=0; i<connections.size(); i++)
            {
                you = (connection) connections.elementAt(i);
                you.out.println(msg);
            }
        }
    }


    connection.java

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    
    import java.net.*;
    import java.io.*;
     
    class connection extends Thread
    {
        protected Socket client;
        protected DataInputStream in;
        protected PrintStream out;
        protected chatserver server;
     
        public connection(chatserver server, Socket client)
        {
            this.server=server;
            this.client=client;
     
            try
            {
                in = new DataInputStream(client.getInputStream());
                out = new PrintStream(client.getOutputStream());
            } catch (IOException e)
            {
                try { client.close(); } catch (IOException e2) {} ;
                System.err.println("Fehler beim Erzeugen der Streams: " + e);
                return;
            }
     
            this.start();
        }
     
     
        public void run()
        {
            String line;
     
            try
            {
                while(true)
                {
                    line=in.readLine();
                    if(line!=null)
                        server.broadcast(line);
                }
            } catch (IOException e)
            {
                System.out.println("Fehler:" + e);
            }
        }
    }


    chatapplet.java

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.applet.*;
     
    public class chatapplet extends Applet implements Runnable
    {
        public static final int PORT = 8765;
        Socket socket;
        DataInputStream in;
        PrintStream out;
        TextField inputfield;
        TextArea outputarea;
        Thread thread;
     
        public void init()
        {
            inputfield = new TextField();
            outputarea = new TextArea();
            outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
            outputarea.setEditable(false);
     
            this.setLayout(new BorderLayout());
            this.add("South", inputfield);
            this.add("Center", outputarea);
     
            this.setBackground(Color.black);
            this.setForeground(Color.black);
            inputfield.setBackground(Color.white);
            outputarea.setBackground(Color.white);
        }
     
        public void start()
        {
            try
            {
                socket = new Socket(this.getCodeBase().getHost(), PORT);
                in = new DataInputStream(socket.getInputStream());
                out = new PrintStream(socket.getOutputStream());
            } catch (IOException e)
            {
                this.showStatus(e.toString());
                say("Verbindung zum Server fehlgeschlagen!");
                System.exit(1);
            }
     
            say("Verbindung zum Server aufgenommen...");
     
            if (thread == null)
            {
                thread = new Thread(this);
                thread.setPriority(Thread.MIN_PRIORITY);
                thread.start();
            }
        }
     
     
        public void stop()
        {
            try
            {
                socket.close();
            } catch (IOException e)
            {
                this.showStatus(e.toString());
            }
     
            if ((thread !=null) && thread.isAlive())
            {
                thread.stop();
                thread = null;
            }
        }
     
     
        public void run()
        {
            String line;
     
            try
            {
                while(true)
                {
                    line = in.readLine();
                    if(line!=null)
                        outputarea.appendText(line+'\n' );
                }
            } catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
        }
     
     
        public boolean action(Event e, Object what)
        {
            if (e.target==inputfield)
            {
                String inp=(String) e.arg;
     
                out.println(inp);
                inputfield.setText("");
                return true;
            }
     
            return false;
        }
     
     
        public void say(String msg)
        {
            outputarea.appendText("*** "+msg+" ***\n");
        }
    }


    Das Ganze läuft auf meinem PC, also ist nicht online.
    Verschiedene Browser habe ich auch schon ausprobiert.
    Würde mich echt freuen, wenn mir hier jemand helfen könnte!
    Danke schonmal!
     

  2. #2
    Avatar von sheel
    sheel sheel ist gerade online Moderator
    tutorials.de Moderator
    Registriert seit
    Jul 2007
    Beiträge
    4.503
    Willkommen bei tutorials.de

    Wenn ein Applet grau bleibt, deutet das meistens auf eine Exception beim Aufbau hin.

    Schau während dem grauen Fenster mal in die Javakonsole, was da gezeigt wird.
     

Ähnliche Themen

  1. Java Skypecasts / Java Entwickler Chat
    Von Thomas Darimont im Forum Java Technology Webconferences
    Antworten: 79
    Letzter Beitrag: 17.12.09, 14:43
  2. [JAVA] Register Programmierung Java Chat
    Von EuropaChat im Forum Stellenangebote (unentgeltlich)
    Antworten: 1
    Letzter Beitrag: 23.06.09, 19:15
  3. Chat mit Java?
    Von russe111 im Forum Java
    Antworten: 3
    Letzter Beitrag: 06.02.08, 21:40
  4. Antworten: 0
    Letzter Beitrag: 16.09.04, 14:40
  5. java - chat
    Von GErD im Forum Javascript & Ajax
    Antworten: 3
    Letzter Beitrag: 24.03.03, 20:03