Thread.start() (run()) excption

Status
Nicht offen für weitere Antworten.
B

blacksh33p

Gast
hiho komischeweise haut er mich plötzlich beim auführen von start() im ServerThread Constructor eine exception raus,.. hat schonmal gefunzt aber ich wes nicht was los ist. ich post mal den aufrun im main, die 2 klassen und die system.out "'s"

Server.java
Code:
package chatserv;

import java.io.*;
import java.net.*;
import java.util.*;
import chatserv.ServerThread;

//import test.ServerThread;


public class Server
{
  // The ServerSocket we'll use for accepting new connections
  private ServerSocket ss;

  
  // A mapping from sockets to DataOutputStreams.  This will
  // help us avoid having to create a DataOutputStream each time
  // we want to write to a stream.
  private Hashtable<Socket, DataOutputStream> outputStreams = new Hashtable<Socket, DataOutputStream>();

  // Constructor and while-accept loop all in one.
  public Server( int port ) throws IOException {

    // All we have to do is listen
    listen( port );
  }

  private void listen( int port ) throws IOException {

	    // Create the ServerSocket
	    ss = new ServerSocket( port );
	    
	    // Tell the world we're ready to go
	    System.out.println( "Listening on "+ss );

	    // Keep accepting connections forever
	    while (true) {

	      // Grab the next incoming connection
	      Socket s = ss.accept();

	      // Tell the world we've got it
	      System.out.println( "Connection from "+s );

	      // Create a DataOutputStream for writing data to the
	      // other side
	      DataOutputStream dout = new DataOutputStream( s.getOutputStream() );

	      // Save this stream so we don't need to make it again
	      outputStreams.put( s, dout );

	      // Create a new thread for this connection, and then forget
	      // about it
	      new ServerThread( this, s );
	    }
  
  }
  
  
//	  Get an enumeration of all the OutputStreams, one for each client
	    // connected to us
	    Enumeration getOutputStreams() {
	      return outputStreams.elements();
	    }

	    // Send a message to all clients (utility routine)
	    void sendToAll( String message ) {
	    	
	    	
	      // We synchronize on this because another thread might be
	      // calling removeConnection() and this would screw us up
	      // as we tried to walk through the list
	      synchronized( outputStreams ) {

	        // For each client ...
	        for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {

	          // ... get the output stream ...
	          DataOutputStream dout = (DataOutputStream)e.nextElement();

	          // ... and send the message
	          try {
	            dout.writeUTF( message );
	          } catch( IOException ie ) { System.out.println( ie ); }
	        }
	      }
	    }

	    // Remove a socket, and it's corresponding output stream, from our
	    // list.  This is usually called by a connection thread that has
	    // discovered that the connectin to the client is dead.
	    void removeConnection( Socket s ) {

	      // Synchronize so we don't mess up sendToAll() while it walks
	      // down the list of all output streamsa
	      synchronized( outputStreams ) {

	        // Tell the world
	        System.out.println( "Removing connection to "+s );

	        // Remove it from our hashtable/list
	        outputStreams.remove( s );

	        // Make sure it's closed
	        try {
	          s.close();
	        } catch( IOException ie ) {
	          System.out.println( "Error closing "+s );
	          ie.printStackTrace();
	        }
	      }
	    }
  
  
  
  
  
}

ServerThread.java
Code:
package chatserv;


import java.io.*;
import java.net.*;


public class ServerThread extends Thread
{
  // The Server that spawned us
  private chatserv.Server server;

  // The Socket connected to our client
  private Socket socket;
  //private boolean logged_in=false;
 // private String[] up;

  // Constructor.
  
  public ServerThread( chatserv.Server server2, Socket socket ) {

    // Save the parameters
    this.server = server2;
    this.socket = socket;

    // Start up the thread
    start();
  }
  public void run() {

	    try {

	      // Create a DataInputStream for communication; the client
	      // is using a DataOutputStream to write to us
	      DataInputStream din = new DataInputStream( socket.getInputStream() );

	      // Over and over, forever ...
	      while (true) {

	        // ... read the next message ...
	        String message = din.readUTF();

	        String para = message.substring(0, 5);
	       // if(para.equals("/LOGIN"))
	        //{
	        //	String temp = message.substring(6, message.length());
	        //	up = temp.split(" ");
	        //	String u = up[0];
	        //	String p = up[1];
	        //	logged_in=haupt.Ui.login(u,p);
	        	
	      // }
	       // if(logged_in)
	        //{
	        // ... tell the world ...
	        System.out.println( "Sending : "+message ); // "+up[0]+"
	        
	        // ... and have the server send it to all clients
	        this.server.sendToAll( message ); //up[0]+" "+
	       // }
	      }
	    } catch( EOFException ie ) {

	      // This doesn't need an error message
	    } catch( IOException ie ) {

	      // This does; tell the world!
	      ie.printStackTrace();
	    } finally {

	      // The connection is closed for one reason or another,
	      // so have the server dealing with it
	      server.removeConnection( socket );
	    }
	  }

}

Ui.java
Code:
private void startChat() throws IOException
	{
		
		new Server(6464);
		
	}


public void actionPerformed(ActionEvent e) 
	{
		String comm = e.getActionCommand().toString();
if(comm.equals("Start"))
		{
			System.out.println("...Server gestartet...");
			try
			{
			startChat();
			
			} catch(IOException x) {}
		      
		}
		
		
	}

System.out
...Server gestartet...
Listening on ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=6464] // dann ist er bei accept und mein ui ist schon //im sack und komplett leer
Exception in thread "Thread-2" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.substring(Unknown Source)
at chatserv.ServerThread.run(ServerThread.java:43)
Sending : /LOGIN blacksh33p 1234
Removing connection to Socket[addr=/192.168.0.8,port=1191,localport=6464]

hoff ich hab euch hier nit zugemüllt, aber ich dacht mir besser als zu wenig *g
 
B

blacksh33p

Gast
ServerThread@44: //String para = message.substring(0, 5);

und er funzt immerhin, aber das ui geht nimmer. graues panel menuleiste weg und frozen jemand ne ahnung?
liegts dran, dass er sich in den actionperformed festfrisst?
 
B

blacksh33p

Gast
ok liegt daran, dass es in den actionperformed gestartet wird.
isses weiterhin inder main, dann bleibt das ui aktiv.. kann den dazu bringen den prozee new Server aus zu lagen, dass die ui weiter geht?
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
OnDemand Thread und Ratelimiter Netzwerkprogrammierung 4
F Thread "extern" beenden Netzwerkprogrammierung 3
OnDemand Linux RAM per Thread Netzwerkprogrammierung 3
V Socket Audio Clip loopen ohne neuen Thread Netzwerkprogrammierung 2
C Thread Netzwerkprogrammierung 9
platofan23 Socket Hilfe mit Socket Thread und ArrayList Netzwerkprogrammierung 6
A RMI RMI - Thread Objekte übertragen und auf anderer Machine weiterlaufen lassen Netzwerkprogrammierung 0
S Socket ThreadPool oder Thread pro Client? Netzwerkprogrammierung 11
T Socket Server starten Thread Problem Netzwerkprogrammierung 12
P Wie spreche ich einen Thread an? Netzwerkprogrammierung 20
M Thread Scheduler Java 5.0? Netzwerkprogrammierung 2
7 Mehrere Verbindungen gleichzeitig in einem Thread mit ApacheHTTP Netzwerkprogrammierung 7
D Socket Dynamische Socket-Thread Erzeugung Netzwerkprogrammierung 2
B Socket Thread handling / Reader init Netzwerkprogrammierung 8
Z Socket Socket-Thread falsch? Netzwerkprogrammierung 5
Helgon Socket Thread Pooling Netzwerkprogrammierung 6
M Was macht Thread, wenn er aus einem "leeren" Inputstream liest ? Netzwerkprogrammierung 5
B callback thread udp Netzwerkprogrammierung 2
D RMI und Thread Netzwerkprogrammierung 4
S Kommunikationsproblem, Thread und manuel Netzwerkprogrammierung 4
U Socket Abhören eines Sockets/Ports in extra Thread Netzwerkprogrammierung 8
K ein Thread pro Stream Netzwerkprogrammierung 2
D Weiterer Thread auf Port 843 horcht Netzwerkprogrammierung 7
D Thread problem Netzwerkprogrammierung 3
S Thread, Daten vom Socket lesen Netzwerkprogrammierung 2
S Thread Transport Netzwerkprogrammierung 6
S Socket Thread in Schleife Netzwerkprogrammierung 4
G Objekte per TCP verschicken + Thread Netzwerkprogrammierung 4
T synchronized mit thread und rückgabe Netzwerkprogrammierung 4
Q Thread und Sockets... Netzwerkprogrammierung 2
2 Class mit ServerSocket erbt von Thread? Netzwerkprogrammierung 3
PAX Outputstream von anderem Thread verwenden lassen Netzwerkprogrammierung 5
C Server mit Multithreading (Thread-Pool) Netzwerkprogrammierung 2
E java.lang.NullPointerException aber nur wenns im Thread läuf Netzwerkprogrammierung 4
F EIN Thread in RMI auf Serverseite Netzwerkprogrammierung 3
G Server Thread beenden Netzwerkprogrammierung 16
T Socket-Thread: Designfrage Netzwerkprogrammierung 14
S Mit Thread arbeitende Maschine Netzwerkprogrammierung 6
T select() ohne NIO - oder wie Worker-Thread sauber beenden? Netzwerkprogrammierung 9
K Selbe Streams mehrfach nutzen (zusätl. Thread) Netzwerkprogrammierung 6
ven000m Thread Frage Netzwerkprogrammierung 11
M Thread läuft nicht weiter Netzwerkprogrammierung 2
B NullPointerException | Thread Netzwerkprogrammierung 4
U Current thread not owner Netzwerkprogrammierung 3
S readline-thread stoppt vorzeitig Netzwerkprogrammierung 6
J RMI - (RemoteException occurred in server thread) Netzwerkprogrammierung 2
S Webserverprogrammierung: Thread-Beschränkung Netzwerkprogrammierung 2
M Exception in thread "main" java.lang.NoClassDefFou Netzwerkprogrammierung 2
F problem mit thread bzw. netzwerkverbindung! Netzwerkprogrammierung 3
L netzwerk mit thread Netzwerkprogrammierung 3
G RMI thread safe ? Netzwerkprogrammierung 11
A Thread gibt Nachrichten mehrmals aus (Messenger in Java) Netzwerkprogrammierung 3
A Classpath Tomcat, Start von Programm aus Tomcat Container Netzwerkprogrammierung 1
precoc Server Socket Start / Stop Netzwerkprogrammierung 5
M Authentifizierung beim Start von Java-Applets Netzwerkprogrammierung 2
M Anzahl Clients aus Start- und Endadr. berechnen Netzwerkprogrammierung 54
L RMI Programm beendet ohne Fehler sofort nach Start (Windows 2000) Netzwerkprogrammierung 7
S RMI-Problem: Nur ein Server start möglich. Netzwerkprogrammierung 40
A RMI Server Start --Schwierigkeiten Netzwerkprogrammierung 8
B Umfangreiche Batch Datei (Rmiregistry prüfung + Prog. start) Netzwerkprogrammierung 4

Ähnliche Java Themen

Neue Themen


Oben