Probleme bei Chatserver

Status
Nicht offen für weitere Antworten.

Lay-C

Aktives Mitglied
Hi ihr,

ich versuche gerade zur Übung einen kleinen Chatserver zu implementieren...

Allerdings will er irgendwie nicht so recht funktionieren und ich weiß nicht warum... ich hoffe ihr könnt mir helfen:

Klasse Server:
Code:
public class Server {
	private ServerSocket ss;
	private ArrayList<Socket> sockets;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length == 1){
			try{
				System.out.println("Binding to Port "+args[0]+"...");
				new Server(Integer.parseInt(args[0]));
			} catch (Exception e) {
				System.out.println("Could not bind to Port: "+args[0]);
				System.out.println("Binding to Port 5000...");
				new Server(5000);
			}
		} else if(args.length == 0){
			System.out.println("Binding to Port 5000...");
			new Server(5000);
		} else {
			System.out.println("Usage: java Server [port]");
		}

	}
	
	
	private Server(int port){
		try {
			ss = new ServerSocket(port);
			sockets = new ArrayList<Socket>();
			System.out.println("Connection is open.");
			while(true){
				Socket s = ss.accept();
				sockets.add(s);
				new ServerThread(this, s).start();
			}
		} catch (IOException e) {
			System.out.println("Could not create: "+ss);
		}
		
	}
	
	public void react(String input){
		sendToAll(input);
	}
	
	private void sendToAll(String message){
		for(Socket s:sockets){
			try {
				System.out.println("Sending \""+message+"\" to "+s);
				new DataOutputStream(s.getOutputStream()).writeBytes(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

Klasse ServerThread:
Code:
public class ServerThread extends Thread{
	private Socket socket;
	private DataInputStream dis;
	private Server server;
	
	public ServerThread(Server serv, Socket s){
		socket = s;
		server = serv;
		System.out.println("New connection "+s);
		try {
			dis = new DataInputStream(socket.getInputStream());
			System.out.println("InputStream open.");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void run(){
		while(true){
			try {
				System.out.println("Reading next Line:");
				String s = dis.readUTF();
				System.out.println(s);
				server.react(s);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Klasse Client:
Code:
public class Client {
	private Socket s;
	private DataOutputStream dos;
	private DataInputStream dis;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Client c = new Client("127.0.0.1", 5000);
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		c.send("Hello\r\n");
		c.send("World!");
	}
	
	private Client(String host, int port){
		try {
			s = new Socket(host, port);
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());
			new Thread(){
				public void run(){
					while(true)
					try {
						System.out.println(dis.readUTF());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}.start();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void send(String message){
		try {
			dos.writeBytes(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Es treten keine Exceptions auf... beim Server wird
"Binding to Port 5000...
Connection is open.
New connection Socket[addr=/127.0.0.1,port=3391,localport=5000]
InputStream open.
Reading next Line:"
ausgegeben.

Beim Client garnichts...

Ich hoffe ihr könnt mir sagen woran das liegt...

mfg,
Lay-C
 
Der Client macht schonmal keine Ausgabe, weil das read im Thread blockiert und wenn ich das richtig sehe, sendest du vom Server nix aus. Dort ist eine react()-Methode (wird hier was gesendet?), die jedoch auch erst nach dem blockierenden read() aufgerufen wird.
Bin mir gerade aber unsicher, warum deine Aufrufe von send aus der main-Methode im Client nicht stattfinden.
 
weil das read im Thread blockiert
damit meinst du das "dis.readUTF()" oder?

Dort ist eine react()-Methode (wird hier was gesendet?)
Da müsste etwas gesendet werden... die react ruft ja die SendToAll auf (ich will das ganze später noch mit Kommandos versehen dass es je nachdem nicht an alle sendet z.B)... aber die ist ja wie gesagt blockiert...

Das Problem liegt dann wahrscheinlich darin, dass die Client.send nicht sendet oder?

Habs jetzt auch nochmal anders versucht:

Die neue main der Clientklasse:
Code:
public static void main(String[] args) {
		final Client c = new Client("127.0.0.1", 5000);
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(){
			public void run(){
				while(true){
					try {
						c.send(new DataInputStream(System.in).readUTF());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
 
Mach mal so:
[HIGHLIGHT="Java"]
new Thread(){
public void run(){
while(true){
try {
c.send("Wurst");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
[/HIGHLIGHT]
Das ist schonmal ein Anfang, weil dann kannst du was sehen.
 
So bekomm ich jetzt nach n paar sekunden was...

kann es sein dass es einfach n bisschen dauert bis der Server "aufnahmefähig" ist?

Edit: allerdings ist die erste Zeile die er bekommt nicht "Wurst" sondern nur "rst"...
 
Zuletzt bearbeitet:
Evtl. nach 5 Sekunden? 😎
[HIGHLIGHT="Java"]
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
[/HIGHLIGHT]
 
ähm... ja xD... hab ich garnichmehr gemerkt dass ich des da rein gemacht hab ^^
jetzt funktionierts...
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben