Socket InputStream wird nicht erzeugt

kaetzacoatl

Bekanntes Mitglied
Hallo,

Ich weiß nicht so richtig was das Problem ist,
daher post ich einfach mal ein bisschen Code:
Java:
public class OTClientController extends Thread {
	
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Distributor dis;

	public OTClientController(Socket client, Distributor dis) throws IOException {
		super("OTClientController");
		this.dis = dis;
		//hier hängt er: java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) line: not available [native method]	
		in = new ObjectInputStream(client.getInputStream());
		out = new ObjectOutputStream(client.getOutputStream());
	}

	@Override
	public void run() {
		Object o;
		try {
			while((o = in.readObject()) != null){
				dis.distribute(o, this);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			throw new Error("Check version! Can't find needed classes!",e);
		}
	}
	
	public void send(Object o) throws IOException{
		out.writeObject(o);
	}

}
 
Der Thread wird nicht durch Überschreiben des Konstruktors gestartet, sonder durch Aufruf der start-Methode.
Die ruft dann run auf - und dort würde ich den Rest einbauen, dann sollte es gehen.

Lies mal hier.

Günter
 
Ich starte den Thread vom Server aus.
Hier mal mehr Code:
Java:
public class Distributor implements Runnable {

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

	ServerSocket server;
	Thread listener;
	Vector<OTClientController> connections;
	
	public Distributor() {
		try {
			server = new ServerSocket(7);
			connections = new Vector<OTClientController>();
			listener = new Thread(this);
			listener.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try{
			while(true) {
				Socket client = server.accept();
				OTClientController c = new OTClientController(client,this);
				c.start();
				connections.addElement(c);
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public void distribute(Object o, OTClientController sender) throws IOException {
		for(OTClientController occ : connections){
			if(!sender.equals(occ))occ.send(o);
		}
	}

}
 
Sieh dir mal diesen Link an:

Java Object Serialization Specification: 3 - Object Input Classes es hat da einen Abschnitt der beschreibt, weshalb du den ObjectOutputStream vor dem ObjectInputStream erzeugen sollst.

Code:
Note - The ObjectInputStream constructor blocks until it completes reading the serialization stream header. Code which waits for an ObjectInputStream to be constructed before creating the corresponding ObjectOutputStream for that stream will deadlock, since the ObjectInputStream constructor will block until a header is written to the stream, and the header will not be written to the stream until the ObjectOutputStream constructor executes. This problem can be resolved by creating the ObjectOutputStream before the ObjectInputStream, or otherwise removing the timing dependency between completion of ObjectInputStream construction and the creation of the ObjectOutputStream.


Aber GP hat schon auch recht, das Lesen des Headers ist noch im gleichen Thread (da im Konstruktor). Den neuen Thread startest du erst danach.
 
Danke!
Ich hab das erzeugen des ObjectInputStreams
jetzt in die run() Methode gelegt, so kann der
Server neue Clients aufnehmen, während der
Thread auf den ObjectOutputStream des
Clients wartet.
 

Neue Themen


Zurück
Oben