Andre267
Erfahrenes Mitglied
Soo irgendwie bekomme ich gerade ständig Exceptiones vom Client...
Hier mal mein Code:
ClientBackend:
ChatClient:
Das hier ist die Exection:
Versteht das jemand?
Hier mal mein Code:
ClientBackend:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.Java.Chat.Client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Andres
*/
public class ClientBackend {
private String antwort;
private String host = "127.0.0.1";
private int port = 9898;
public void clientsend(String message) {
//Socket erstellen der an Server sendet und port 9898
//try with resources schließt die verbindung automatisch! (unseren socket)
try (Socket socket = new Socket(this.host, this.port)) {
//Input & Output Stream erstellen zum empfangen und senden
//DataInputStream dataIn = new DataInputStream(socket.getInputStream());
DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
//Naricht defenieren
dataOut.writeUTF(message);
//Naricht die empfangen wird speichern
//String antwort = dataIn.readUTF();
//Antwort vom Server ausgeben
//System.out.println(antwort);
} catch (UnknownHostException ex) {
Logger.getLogger(ClientBackend.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClientBackend.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String clientread() {
while (true) {
//Socket erstellen der an Server sendet und port 9898
//try with resources schließt die verbindung automatisch! (unseren socket)
try (Socket socket = new Socket(this.host, this.port)) {
//Input Stream erstellen zum empfangen
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
//Naricht die empfangen wird speichern
this.antwort = dataIn.readUTF();
//Antwort vom Server ausgeben
return this.antwort + "\n";
} catch (UnknownHostException ex) {
Logger.getLogger(ClientBackend.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClientBackend.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
ChatClient:
Code:
package de.Java.Chat.Client;
/**
*
* @author Andres
*/
public class ChatClient extends javax.swing.JFrame {
/**
* Creates new form ChatClient
*/
public ChatClient() {
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() {
UserName = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
ChatView = new javax.swing.JTextArea();
submit = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
Eingabe = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
Warnings = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Andres Privater Chat");
setResizable(false);
ChatView.setEditable(false);
ChatView.setColumns(20);
ChatView.setLineWrap(true);
ChatView.setRows(5);
ChatView.setWrapStyleWord(true);
jScrollPane1.setViewportView(ChatView);
submit.setText("Senden");
submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitActionPerformed(evt);
}
});
Eingabe.setColumns(20);
Eingabe.setLineWrap(true);
Eingabe.setRows(3);
Eingabe.setToolTipText("Tippen Sie hier Ihre Nachricht ein!");
Eingabe.setWrapStyleWord(true);
jScrollPane2.setViewportView(Eingabe);
jLabel2.setText("User Name:");
javax.swing.GroupLayout WarningsLayout = new javax.swing.GroupLayout(Warnings);
Warnings.setLayout(WarningsLayout);
WarningsLayout.setHorizontalGroup(
WarningsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 83, Short.MAX_VALUE)
);
WarningsLayout.setVerticalGroup(
WarningsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Warnings, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(UserName)
.addComponent(submit, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
.addGap(20, 20, 20)))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(UserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Warnings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(52, 52, 52)
.addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(26, 26, 26))
);
pack();
}// </editor-fold>
private void submitActionPerformed(java.awt.event.ActionEvent evt) {
if (!UserName.getText().equals(null) && !Eingabe.getText().equals(null)) {
//ChatView.append("[" + UserName.getText() + "] " + Eingabe.getText() + "\n");
String naricht = "[" + UserName.getText() + "] " + Eingabe.getText() + "\n";
chatSystem.clientsend(naricht);
Eingabe.setText("");
String antwort = chatSystem.clientread();
ChatView.append(antwort);
} else {
//Popup Warnung
// JOptionPane.showMessageDialog(frame,
// "Bitte geben Sie ihren Usernamen und eine Naricht ein!",
// "Eingabe Fehler",
// JOptionPane.ERROR_MESSAGE);
}
}
/**
* @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatClient.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 ChatClient().setVisible(true);
}
});
}
//Das Client Backend implementieren
private static ClientBackend chatSystem = new ClientBackend();
// Variables declaration - do not modify
private javax.swing.JTextArea ChatView;
private javax.swing.JTextArea Eingabe;
private javax.swing.JTextField UserName;
private javax.swing.JPanel Warnings;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton submit;
// End of variables declaration
}
Das hier ist die Exection:
Code:
Okt 01, 2012 12:07:39 PM de.Java.Chat.Client.ClientBackend clientread
SEVERE: null
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at de.Java.Chat.Client.ClientBackend.clientread(ClientBackend.java:55)
at de.Java.Chat.Client.ChatClient.submitActionPerformed(ChatClient.java:125)
at de.Java.Chat.Client.ChatClient.access$000(ChatClient.java:7)
at de.Java.Chat.Client.ChatClient$1.actionPerformed(ChatClient.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
BUILD STOPPED (total time: 8 seconds)
Versteht das jemand?