Posted By:
A_D
Posted On:
Monday, February 20, 2006 07:23 PM
how can i invoke to run a method declared in main without using static class atributes? beacuse if i invoke the method in the same class no problem but if i invoke inside the thread class in the method run it throws me a NullPointerException any tip????? many thanks...... there's my source instead Please use html tags to format code blocks. import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; import java.net.SocketException; import java.util.LinkedList; import java.util.Li
More>>
how can i invoke to run a method declared in main without using static class atributes? beacuse if i invoke the method in the same class no problem but if i invoke inside the thread class in the method run it throws me a NullPointerException any tip????? many thanks...... there's my source instead
Please use html tags to format code blocks.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.LinkedList;
import java.util.ListIterator;
public class ServerEx
{
public ListIterator ite = null;
public LinkedList reg_thread = null;
public SocketThread clients = null;
public ServerSocket socketServer = null;
/*_____________________________________________________________________________
*
*
* M A I N
*
*___________________________________________________________________________*/
public static void main (String argv [ ]) throws IOException
{
ServerEx server = new ServerEx();
Socket socketClient = null;
int Port = 0,
count = 0;
/*---command line arguments---*/
if( argv.length != 2 )
{
System.err.println( "usage: java ServerEx -port
<#>" );
System.exit ( 0 );
}
try
{
Port = Integer.parseInt ( argv [ 1 ] );
}
catch( NumberFormatException e)
{
System.err.println ("
The port must be a number: "+
e.toString ( ));
System.err.println( "usage: java ServerEx -port
<#>" );
System.exit ( 0 );
}
if ( Port
< 1 || Port > 65535 )
{
System.err.println( "
port must be between 1 and 65535
" +
"usage: java ServerEx -port
<#>");
System.exit ( 0 );
}
else if (argv [ 0 ].compareTo ("-port") !=0 )
{
System.err.println( "
invalid option '"+ argv [ 0 ] + "'
" +
"usage: java ServerEx -port
<#>");
System.exit ( 0 );
}
try
{
server.socketServer = new ServerSocket( Port );
System.out.println("Server created. Waiting client connections...");
}
catch (IOException e)
{
System.err.println("could not listen on that port " + e.toString());
System.exit (0);
}
boolean listening_connection = true;
server.reg_thread = new LinkedList();
while (listening_connection)
{
try
{
socketClient = server.socketServer.accept();
server.clients = new SocketThread (socketClient, count);
server.clients.start();
System.out.println("
client: " + count + " accepted " );
count++;
server.reg_thread.add( server.clients );
//server.clients.Stop();
}
catch (SocketException e)
{
System.err.println ("the server has been closed" );
listening_connection = false;
}
server.Shutdown();
}
server.clients.interrupt();
}/*____________________________ END MAIN __________________________________*/
public void Shutdown()
{
ite = reg_thread.listIterator();
while (ite.hasNext ())
{
clients = (SocketThread) ite.next ();
System.out.println (clients.toString());
}
}
}/*_________________________ END ServerEx class______________________________*/
/*===========================================================================*/
import java.net.Socket;
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.IOException;
public class SocketThread extends Thread
{
private ServerEx srv = null;
private PrintWriter output = null;
private BufferedReader in = null;
private String receive_str = null;
private Socket client_socket = null;
private int number_of_client = 0;
private boolean receiving = true;
public SocketThread ( Socket socketClient, int client_number )
{
client_socket = socketClient;
number_of_client = client_number;
}
/*_____________________________________________________________________________
*
*
* R U N
*
*___________________________________________________________________________*/
public void run ()
{
try
{
output = new PrintWriter (client_socket.getOutputStream ( ) ,true );
in = new BufferedReader
(new InputStreamReader( client_socket.getInputStream( ) ) );
}
catch (IOException e)
{
System.err.println ( e.toString());
}
receiving = true;
System.out.println ("client address: " +
client_socket.getInetAddress () +
":" +
client_socket.getPort () + " local port : " +
client_socket.getLocalPort ());
try
{
while ( receiving )
{
receive_str = in.readLine();
output.println(" message from client: " + number_of_client +
" sent: " +
""" + receive_str + """);
System.out.println ("client: " + number_of_client +
" sends : " + """ +
receive_str + """);
Shutdown();
Shutdown();
Shutdown();
if (receive_str.compareToIgnoreCase ("exit") == 0)
{
client_socket.close();
System.out.println("
the client: " + number_of_client +
" has been closed");
receiving = false;
}
else
if (receive_str.compareToIgnoreCase ("shutdown") == 0)
{
srv.socketServer.close();
}
}
}
catch (IOException e)
{
System.err.println("the client: " + number_of_client +
" has been closed");
}
}/*_____________________________END OF RUN_________________________________*/
/*________________________________STOP______________________________________*/
public void Stop()
{
try
{
client_socket.close();
}
catch (IOException e)
{
System.err.println(e.toString());
}
receiving = false;
}/*_____________________________END STOP___________________________________*/
}/*_________________________END SocketThread class___________________________*/
/*===========================================================================*/
<<Less