was ich will ist dass, jeder in den Chat kommen soll, und der Chat selbst auf dem Port festgelegt ist,
Also ich weiß nicht, irgendwie muss es doch möglich sein, einen Chat zu schreiben,
du hattest gesagt der braucht einen Host, gehts nicht das jeder sein eigener Host ist,
und das Treffen in der Port adresse stattfindet?
Das wäre viel besser
ich habs auch schon ausprobiert,
diesen Port"127.0.0.1" anzugeben.
doch immer wenn ich beim "client" einen Text Eingebe und Bestätige kommt folgende FehlerMeldung
PHP:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Profi_Chat.Client.processMessage(Client.java:65)
at Profi_Chat.Client$1.actionPerformed(Client.java:38)
at java.awt.TextField.processActionEvent(Unknown Source)
at java.awt.TextField.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Ich denke mitlerweile hast du vielleicht auch den code, nur mal so,
ich hab die Zeilen nicht verändern, wie sie in meinem ersten Anfangs
Post schon gesendet wurde,
also was mache ich falsch, prinzipielle mach ich alles so wie du gesagt hast,
PS:
Wie macht man Java Code Tags hier in diesem Forum?
Code:
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JFrame;
public class Client extends JFrame implements Runnable {
// Components for the visual display of the chat windowsprivate
TextField tf = new TextField();
private final TextArea ta = new TextArea();
// The socket connecting us to the serverprivate
Socket socket;
// The streams we communicate to the server; these come
// from the socketprivate
DataOutputStream dout;
protected DataInputStream din;
// Constructorpublic
public Client(final String host, final int port) {
// Set up the screen
setLayout(new BorderLayout());
setVisible(true);
setSize(200,200);
add("North", this.tf);
add("Center", this.ta);
// We want to receive messages when someone types a line
// and its return, using an anonymous class as
// a callback
this.tf.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
processMessage(e.getActionCommand());
// Connect to the server
try {
// Initiate the
Client.this.socket = new Socket("", 12);
// We got a connection! Tell the world
System.out.println("connected to " + Client.this.socket);
// Let's grab the streams and create DataInput/Output
// streams
// from them
Client.this.din = new DataInputStream(Client.this.socket.getInputStream());
Client.this.dout = new DataOutputStream(Client.this.socket.getOutputStream());
}
// Start a background thread for receiving messagesnew
catch (final IOException ie) {
System.out.println(ie);
}
}
});
}
// Gets called when the user types something
protected void processMessage(final String message) {
try {
// Send it to the server
this.dout.writeUTF(message);
// Clear out text input field
this.tf.setText("");
} catch (final IOException ie) {
System.out.println(ie);
}
}
// Background thread runs this: show messages from other window
public void run() {
try {// Receive messages one-by-one, forever
while (true) {
// Get the next
final String message = this.din.readUTF();
// Print it to our text
this.ta.append(message + "\n");
}
} catch (final IOException ie) {
System.out.println(ie);
}
}
public static void main(String[]args)
{
new Client("127.0.0.1",2);
}
}
Ich hab nicht ganz verstanden was du mit fatalem Fehler meinst,
könntest du mir in deinem nächsten Post als letztes bitte den richtigen Code
senden