# Problem mit "Chat"



## KSG9|sebastian (4. Jul 2005)

Hi Leute..


ich hab grad versucht nen kleinen Chat zu proggen. Ich kann mit dem Client auch connecten und 1x was schicken. Aber dann nicht mehr. Ich krieg keine Exception, gar nix, aber im Client-Thread geht glaub ich was schief.
Auserdem wird das Programm nicht sauber beendet...bzw. gar nicht beendet  ich muss es imm über Eclipse "terminaten"

Code;

Server


```
package de.fiducia.server;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class Server extends JFrame implements Runnable {
	private int port = 80;

	private ServerSocket serverSock;

	private JSplitPane pane;
	
	private Vector clients;

	private JList cls;

	private JTextArea traffic;

	private Thread t;

	public final String CN_HELLO = "hello";

	public Server() {
		super("Server");
		super.setBounds(20, 20, 450, 250);

		clients = new Vector();
		traffic = new JTextArea();
		cls = new JList(clients);

		traffic.setBorder(new TitledBorder(new LineBorder(Color.black), "Traffic"));
		cls.setBorder(new TitledBorder(new LineBorder(Color.black), "Clients"));
		super.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent arg0) {
				shutdownClients();
				close();
				dispose();
			}
		});

		//super.getContentPane().add(traffic, BorderLayout.CENTER);
		//super.getContentPane().add(cls, BorderLayout.EAST);
			
		pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(traffic), cls);
		pane.setDividerLocation(320);
		
		super.getContentPane().setLayout(new GridLayout(1, 1));
		super.getContentPane().add(pane);
		
		
		super.setVisible(true);
		try {
			serverSock = new ServerSocket(port);
			traffic.append("ServerSocket opened on port " + port + "...\n");
			traffic.append("ServerSocket is waiting for connections...\n");
		} catch (IOException e) {
			shutdownClients();
			e.printStackTrace();
		}
		t = new Thread(this);
		t.start();
	}
	private void close(){
		t.interrupt();
	}
	private void shutdownClients() {
		t.interrupt();
		for (int i = 0; i < clients.size(); i++) {
			if(clients.get(i) != null)
				((ServerUser) clients.get(i)).close();
		}
	}

	public static void main(String[] args) {

	}

	public void append(String text) {
		traffic.append(text + "\n");
	}

	private void initiateConnection(Socket s) {
		traffic.append("Client connected from "
				+ s.getInetAddress().getHostAddress() + "\n");
		PrintWriter writer = null;
		BufferedReader reader = null;
		try {
			writer = new PrintWriter(s.getOutputStream());
			reader = new BufferedReader(new InputStreamReader(s
					.getInputStream()));
			writer.println(CN_HELLO);
			writer.flush();
			String name = reader.readLine();
			if (null == name || name.trim().toLowerCase().equals("")) {
				traffic.append("Error\n");
			} else {
				ServerUser user = new ServerUser(this, s, name);
				clients.add(user);
				traffic.append("Client " + name + " added...\n");
				cls.setListData(clients);
			}

		} catch (IOException e) {
			traffic.append("Error " + s.getInetAddress().getHostAddress()
					+ "\n");
		}
	}

	public void run() {

		while (true) {
			try {
				while (!t.isInterrupted()) {
					Socket s = serverSock.accept();
					initiateConnection(s);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	public void broadcast(String text) {
		for (int i = 0; i < clients.size(); i++) {
			ServerUser u = (ServerUser) clients.get(i);
			u.print(text);
		}
	}
}
```

Client


```
package de.fiducia.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import de.fiducia.jitc.JustInTimeCompiler;

public class Client extends Thread {
	private int port = 80;

	private String host = "10.2.184.42";

	private Socket socket;

	private JustInTimeCompiler jitc;

	private BufferedReader reader = null;

	private PrintWriter writer = null;

	public final String CN_HELLO = "hello";

	public Client() {
		// jitc = new JustInTimeCompiler();
		connect();
		this.start();
	}

	private void connect() {
		try {
			socket = new Socket(host, port);

			reader = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			writer = new PrintWriter(socket.getOutputStream());

		} catch (UnknownHostException e) {
			close();
			e.printStackTrace();
		} catch (IOException e) {
			close();
			e.printStackTrace();
		}
	}

	public void run() {

		while (true) {
			String line = "";
			try {
				line = reader.readLine();
			} catch (IOException e) {
				close();
				e.printStackTrace();
			}
			if (CN_HELLO.equals(line)) {
				writer.println("Sebastian");
				writer.flush();
			} else {
				writer.println("texttexttext");
				writer.flush();
			}
			line = "";
		}
	}

	private void close() {
		try {
			this.interrupt();

			if (reader != null)
				reader.close();
			if (writer != null)
				writer.close();
			if (socket != null)
				socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Client();
	}
}
```


Klasse mit der ein User am Server "behandelt" wird



```
package de.fiducia.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerUser extends Thread {
	private Server server;
	private Socket socket;
	private PrintWriter writer;
	private BufferedReader reader;
	private String name;
	
	public ServerUser(Server server, Socket socket, String name){
		this.server = server;
		this.socket = socket;
		this.name = name;
		
		try {
			writer = new PrintWriter(socket.getOutputStream());
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		this.start();

	}
	public void print(String text){
		writer.println(text);
		writer.flush();
	}
	public void run(){
		String input = null;
		while(socket != null && !isInterrupted()){
			try {
				input = reader.readLine();
				System.out.println("input: ");
				System.out.println(input);
				
				server.append("Message " + input + " received.\n");
				
				if(input != null && !input.trim().toLowerCase().equals(""))
					server.broadcast(input);
				
			} catch (IOException e) {
				interrupt();
				e.printStackTrace();
			}
		}
		
		if(socket == null || socket.isClosed()){
			this.interrupt();
		}
		
	}
	public void close(){
		try {
			this.interrupt();
			if(reader != null)
				reader.close();
			if(writer != null)
				writer.close();
			if(socket != null);
				socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public String toString(){
		return name;
	}

}
```


Bin für jede Hilfe dankbar...ach ja..verbesserungsvorschläge von wegen Geschwindigkeit u.s.w. wären nett.

Gruß Sebastian


----------



## KSG9|sebastian (4. Jul 2005)

ach ja....starten tu ich dei dinger natürlich mit

new Server()
new Client()


----------



## KSG9|sebastian (4. Jul 2005)

niemand ne idee?


----------

