Hallo,
ich habe 2 Probleme! Erstens, warum wird die Methode
(Client) nicht aufgerufen? Habe die Klasse um Thread erweitert?
2. Warum empfängt der Client nur seine eigenen Eingaben und nicht alle anderen auch? Ist das ein Server- oder Clientproblem?
Hier der Code (ja ich weiß, ist noch verbesserungsbedürftig - mir gehts aber erstmal ums Prinzip):
Server:
ServerThread:
Client:
ich habe 2 Probleme! Erstens, warum wird die Methode
Code:
public void run()
2. Warum empfängt der Client nur seine eigenen Eingaben und nicht alle anderen auch? Ist das ein Server- oder Clientproblem?
Hier der Code (ja ich weiß, ist noch verbesserungsbedürftig - mir gehts aber erstmal ums Prinzip):
Server:
Java:
package name.purzel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Chat_Server {
public static final int PORT = 8765;
//public static final String HOST = "localhost";
public static final String HOST = "masterpurzel.dyndns-server.com";
public static String USERNAME = "tester85";
static JFrame f;
static JMenuBar menu;
static JMenu menu_file;
static JMenu menu_help;
static JMenuItem menuItem_settings;
static JMenuItem menuItem_exit;
static JMenuItem menuItem_faq;
static JMenuItem menuItem_about;
static JButton connect;
static JButton disconnect;
static JLabel status;
static ServerSocket server;
static Socket s;
public static void main(String[] args) {
/*
* Layout
*/
f = new JFrame("Java Chat-Server");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(750, 450);
/*
* Menü Anfang
*/
menu = new JMenuBar();
menu_file = new JMenu("File");
menu_help = new JMenu("Help");
menuItem_settings = new JMenuItem("Settings");
menuItem_settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menuItem_exit = new JMenuItem("Exit");
menuItem_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
server.close();
} catch (IOException se) {
se.printStackTrace();
} finally {
System.exit(0);
}
}
});
menuItem_faq = new JMenuItem("F.A.Q.");
menuItem_faq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menuItem_about = new JMenuItem("About");
menuItem_about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menu.add(menu_file);
menu.add(menu_help);
menu_file.add(menuItem_settings);
menu_file.add(menuItem_exit);
menu_help.add(menuItem_faq);
menu_help.add(menuItem_about);
/*
* Menü Ende
*/
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
f.add(menu, c);
connect = new JButton("Connect");
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
connect.setEnabled(false);
disconnect.setEnabled(true);
status.setText("Connected");
try {
server = new ServerSocket(PORT);
new Thread(new Runnable() {
public void run() {
s = new Socket();
while(!s.isClosed()) {
try {
s = server.accept();
} catch (IOException e) {
e.printStackTrace();
break;
}
ServerThread t = new ServerThread(s);
t.start();
}
}
}).start();
} catch (IOException se) {
se.printStackTrace();
}
}
});
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
f.add(connect, c);
disconnect = new JButton("Disconnect");
disconnect.setEnabled(false);
disconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
connect.setEnabled(true);
disconnect.setEnabled(false);
status.setText("Disconnected");
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
f.add(disconnect, c);
status = new JLabel("Disconnected");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
f.add(status, c);
f.pack();
f.setVisible(true);
new Chat_Server();
}
}
ServerThread:
Java:
package name.purzel;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ServerThread extends Thread {
public Socket s;
BufferedReader in;
String text;
BufferedWriter out;
public ServerThread(Socket s) {
this.s = s;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while((text = in.readLine()) != null) {
try {
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write(s + ": "+ text);
out.newLine();
out.flush();
if(s.isClosed()) {
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
Java:
package name.purzel;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Chat_Client extends Thread {
static String HOST = "masterpurzel.dyndns-server.com";
static int PORT = 8765;
static JFrame f;
static JMenuBar menu;
static JMenu menu_file;
static JMenu menu_help;
static JMenuItem menuItem_settings;
static JMenuItem menuItem_exit;
static JMenuItem menuItem_faq;
static JMenuItem menuItem_about;
static JTextArea output;
static JTextField input;
public static String text;
public static String text2;
public static Socket s;
public static BufferedWriter out;
public static BufferedReader in;
/**
* @param args
*/
public static void main(String[] args) {
/*
* Layout
*/
f = new JFrame("Java Chat-Client");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(750, 450);
/*
* Menü Anfang
*/
menu = new JMenuBar();
menu_file = new JMenu("File");
menu_help = new JMenu("Help");
menuItem_settings = new JMenuItem("Settings");
menuItem_settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menuItem_exit = new JMenuItem("Exit");
menuItem_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuItem_faq = new JMenuItem("F.A.Q.");
menuItem_faq.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menuItem_about = new JMenuItem("About");
menuItem_about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menu.add(menu_file);
menu.add(menu_help);
menu_file.add(menuItem_settings);
menu_file.add(menuItem_exit);
menu_help.add(menuItem_faq);
menu_help.add(menuItem_about);
/*
* Menü Ende
*/
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
f.add(menu, c);
output = new JTextArea(35, 60);
JScrollPane scrollbar = new JScrollPane(output);
output.setFont(new Font("Dialog", Font.PLAIN, 12));
output.setEditable(false);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
f.add(scrollbar, c);
input = new JTextField();
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text = input.getText().toString().trim();
if(text != null || !text.equals("") || !text.isEmpty()) {
try {
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write(text);
out.newLine();
out.flush();
input.setText("");
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch(NullPointerException e1) {
e1.printStackTrace();
}
}
}
});
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
f.add(input, c);
f.pack();
f.setVisible(true);
new Chat_Client();
}
public Chat_Client() {
try {
s = new Socket(HOST, PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
public void run() {
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while((text2 = in.readLine()) != null) {
text2 = text2.trim();
output.append(text2 + "\n");
//System.out.print("Received: ");
//System.out.println(text2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void run() { //geht nicht, warum?
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while((text2 = in.readLine()) != null) {
text2 = text2.trim();
output.append(text2);
System.out.print("Received: ");
System.out.println(text2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}