Chatserver not working like it should
Hi
I have created a chatserver and a chatclient where the chatserver is running as a multithreaded server where is can receive multiple requests at a time instead of only being able to handle one request at a time.
When i start up the server, it works fine and then i start up the client which seems to work fine, but on the server side im getting a "NullPointerException". I think that the server doesnt find the socket to the client.
This is the chatserver code:
/**
Filename: ChatServer.java
Last updated: 2007-02-01
@author chrisrus
@version 0.5a
*/
/**Class that represents a running server */
import java.net.* ;
import java.io.* ;
publicclass ChatServerimplements Runnable
{
// Needed variables for establishing connection
ServerSocket srvSocket ;
Socket clnSocket ;
PrintWriter pw ;
/**
Method for starting up the server
*/
publicvoid startUpServer()
{
try
{
srvSocket =new ServerSocket(4242) ;
System.out.println("Server running on port: 4242.") ;
while(true)
{
ChatServer cs =new ChatServer() ;
Thread newRequest =new Thread(cs) ;
clnSocket = srvSocket.accept() ;
newRequest.start() ;
}
}
catch(IOException io)
{
System.out.println("Error handling the client request.") ;
}
}
/**
Method executed by a thread
*/
publicvoid run()
{
try
{
pw =new PrintWriter(clnSocket.getOutputStream());
for (int i = 0 ; i < 50 ; i ++ )
{
pw.write("Test message from server") ;
}
pw.close() ;
}
catch (IOException io)
{
System.out.println("Error when sending information to the client..") ;
}
catch (NullPointerException nu)
{
System.out.println("Error") ;
nu.printStackTrace() ;
}
}
}
How can i fix this so the server will be able to send the message to the client and why is this failing? Is it because the method "run()" cant access the clients output stream, if so, why doesnt i get to access this and how would i fix this..
Any help i appericiated. Thanks.

