Hallo!
Hab so ein kleines Chatprog geschrieben.
Wenn ich das Prog schließe kommt folgende Fehlermeldung:
Hier der Code:
Und wenn ich die Komponenten in J ändere sehe ich gar nichts.
Woran liegt so etwas?
Gruß Ronn
Hab so ein kleines Chatprog geschrieben.
Wenn ich das Prog schließe kommt folgende Fehlermeldung:
java.net.SocketException: socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:353)
at java.net.ServerSocket.implAccept(ServerSocket.java:439)
at java.net.ServerSocket.accept(ServerSocket.java:410)
at ChatServer.runChat(ChatServer.java:216)
at ChatServer.<init>(ChatServer.java:53)
at ChatServer.main(ChatServer.java:228)
Hier der Code:
Code:
public class ChatServer extends JFrame implements ActionListener
{
private Panel pElemente;
private Label lCounter;
private Label lPortadresse;
private TextArea taAusgabe;
private Button bShowChat;
private Button bBeenden;
public static int counter = 0;
private ServerSocket server;
static Vector vecClient = new Vector();
public ChatServer() throws Exception
{
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName() );
}
catch(Exception e)
{
e.printStackTrace();
}
setSize(300,100);
setVisible(true);
setResizable(false);
setLocation(300,160);
setTitle("Chat-Server");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
server.close();
System.exit(0);
}catch(IOException fehler){}
}
});
fensterkomponenten();
server = new ServerSocket(5555);
runChat();
}
private void fensterkomponenten()
{
String portadresse = "5555";
this.getContentPane().setLayout(null);
pElemente = new Panel();
pElemente.setLayout(null);
pElemente.setBounds(0,0,300,300);
lCounter = new Label();
lCounter.setText("Verbundene Clients : " +counter);
lCounter.setBounds(0,0,150,25);
pElemente.add(lCounter);
lPortadresse = new Label();
lPortadresse.setText("Portadresse : " +portadresse);
lPortadresse.setBounds(180,0,120,25);
pElemente.add(lPortadresse);
bBeenden = new Button("Beenden");
bBeenden.setBounds(10,30,80,25);
bBeenden.addActionListener(this);
pElemente.add(bBeenden);
bShowChat = new Button("Chat öffnen");
bShowChat.setBounds(200,30,90,25);
bShowChat.addActionListener(this);
pElemente.add(bShowChat);
taAusgabe = new TextArea();
taAusgabe.setBounds(10,90,280,150);
pElemente.add(taAusgabe);
this.getContentPane().add(pElemente);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equalsIgnoreCase("Chat öffnen"))
{
this.setSize(300,300);
bShowChat.setLabel("Chat schließen");
}
if(e.getActionCommand().equalsIgnoreCase("Chat schließen"))
{
// Textarea einblenden
this.setSize(300,100);
bShowChat.setLabel("Chat öffnen");
}
if(e.getActionCommand().equalsIgnoreCase("Beenden"))
{
try
{
if(JOptionPane.showConfirmDialog(this,"Wollen Sie den Dienst wirklich beenden ?","Frage ???",JOptionPane.YES_NO_OPTION)==0)
{
try
{
server.close();
System.exit(0);
}catch(Exception enet){}
}
}
catch(Exception a)
{}
}
}
//innere Klasse
class ClientConnect extends Thread
{
String name = "noname";
BufferedReader reader;
BufferedWriter writer;
public ClientConnect(Socket client)
{
ChatServer.counter++;
setzeCounter();
try
{
reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
name = reader.readLine();
}
catch(IOException erw){}
}
public void run()
{
String zeile;
try
{
while((zeile = reader.readLine()) != null)
{
taAusgabe.append(name + " : " + zeile + '\n');
doSende(zeile);
}
}
catch(IOException iof)
{
System.out.println(iof);
}
ChatServer.counter--;
setzeCounter();
try
{
reader.close();
writer.close();
writer.flush();
}
catch(Exception fe)
{
System.out.println(fe);
}
}
public void doSende(String text)
{
for(int i=0 ; i < vecClient.size() ; i++)
{
ClientConnect cc =((ClientConnect)vecClient.elementAt(i));//.sende(name + " : " + text);
try
{
cc.writer.write(text);
cc.writer.newLine();
cc.writer.flush();
}
catch(IOException fehler)
{
System.out.println(fehler);
}
}
}
}//Ende innere Klasse
public void setzeCounter()
{
lCounter.setText("Verbundene Clients : " +counter);
}
public void runChat() throws IOException
{
while(true)
{
ClientConnect clientc = new ClientConnect(server.accept());
vecClient.addElement(clientc);
clientc.start();
}
}
public static void main(String[] args)
{
ChatServer chatS;
try
{
chatS = new ChatServer();
chatS.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Woran liegt so etwas?
Gruß Ronn