# Thread.start() (run()) excption



## blacksh33p (18. Jan 2007)

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*

```
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*

```
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*

```
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)
> ...



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


----------



## blacksh33p (18. Jan 2007)

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?


----------



## blacksh33p (18. Jan 2007)

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?


----------

