G
Guest
Gast
Hallo,
ich möchte einen kleinen messenger erstellen.
ServerStart.java
wie kann ich denn einen neu angelgten thread beenden? sollte ich noch mehr ändern? ist mein ansatz falsch?
ich möchte einen kleinen messenger erstellen.
ServerStart.java
Code:
import java.awt.event.ActionListener;
/*
* Server.java
*
* Created on 7. Juli 2008, 10:49
*/
/**
*
* @author bmr
*/
public class ServerStart extends javax.swing.JFrame implements ActionListener {
Server s = new Server();
/** Creates new form Server */
public ServerStart() {
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() {
mainPanel = new javax.swing.JPanel();
serverStarten = new javax.swing.JButton();
serverBeenden = new javax.swing.JButton();
statusLabel = new javax.swing.JLabel();
serverStatus = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Bundle"); // NOI18N
setTitle(bundle.getString("ServerStart.title")); // NOI18N
setName("ServerFrame"); // NOI18N
setResizable(false);
serverStarten.setText(bundle.getString("ServerStart.serverStarten.text")); // NOI18N
serverStarten.addActionListener(this);
serverBeenden.setText(bundle.getString("ServerStart.serverBeenden.text")); // NOI18N
serverBeenden.addActionListener(this);
statusLabel.setText(bundle.getString("ServerStart.statusLabel.text")); // NOI18N
serverStatus.setText(bundle.getString("ServerStart.serverStatus.text")); // NOI18N
org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(54, 54, 54)
.add(statusLabel)
.add(57, 57, 57)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(serverBeenden, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(serverStarten, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(serverStatus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(18, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(30, 30, 30)
.add(serverStarten)
.add(18, 18, 18)
.add(serverBeenden)
.add(54, 54, 54)
.add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(statusLabel)
.add(serverStatus))
.addContainerGap(127, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
// Code for dispatching events from components to event handlers.
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == serverStarten) {
ServerStart.this.serverStartenActionPerformed(evt);
}
else if (evt.getSource() == serverBeenden) {
ServerStart.this.serverBeendenActionPerformed(evt);
}
}// </editor-fold>
private void serverStartenActionPerformed(java.awt.event.ActionEvent evt) {
if((!s.isStarted)) {
s.start();
s.setOnline();
serverStatus.setText("Server gestartet");
} else {
serverStatus.setText("Server ist bereits gestartet");
}
mainPanel.validate();
mainPanel.repaint();
}
private void serverBeendenActionPerformed(java.awt.event.ActionEvent evt) {
s.interrupt();
serverStatus.setText("Server beendet");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerStart().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel mainPanel;
private javax.swing.JButton serverBeenden;
private javax.swing.JButton serverStarten;
private javax.swing.JLabel serverStatus;
private javax.swing.JLabel statusLabel;
// End of variables declaration
}
Code:
import java.net.*;
import java.io.*;
public class Server extends Thread {
Boolean isStarted = false;
@Override
public synchronized void run(){
while (isStarted) {
try {
ServerSocket server = new ServerSocket(4000);
Socket client = server.accept();
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
String name = in.readUTF();
String passwort = in.readUTF();
System.out.println(name + passwort);
out.writeUTF("ok");
out.flush();
in.close();
out.close();
} catch (Exception e) {
System.out.println("Fehler im Server");
}
}
}
public void setOffline() {
isStarted = false;
}
public void setOnline() {
isStarted = true;
}
}
wie kann ich denn einen neu angelgten thread beenden? sollte ich noch mehr ändern? ist mein ansatz falsch?