|
-
Sockets and BufferedWriter
Hello i am totally stumped at this one. I've got 2 classes genusIMServer and genusClientHandler (below). When I connect to port 2233 using telnet and enter 'UserLogin;' and press enter i get a NullPointerException with BufferedWriter outStream. I have no idea why. Could anyone enlighten me? The error occurs in the ProcessInput() method in the genusClientHandler class. Here is the code for both classes:
//--------------------------------------------------------------------
import java.net.*;
import java.io.*;
class genusIMServer{
private ServerSocket ssMain;
private boolean IsListening = true;
public genusIMServer(){
System.out.println("-------------------------------------------");
System.out.println("-------- Welcome to genusIM server --------");
System.out.println("-------------------------------------------\n");
try{
ssMain = new ServerSocket(2233);
System.out.println("Server started...");
}
catch(IOException e){}
StartListening();
}
public static void main(String[] arguments){
genusIMServer IMServer = new genusIMServer();
}
public void StartListening(){
while(IsListening){
try{
new genusClientHandler(ssMain.accept()).start();
}
catch(IOException e){}
}
}
}
//--------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class genusClientHandler extends Thread{
private Socket sckMain;
private BufferedWriter outStream;
private BufferedReader inStream;
public genusClientHandler(Socket socket){
sckMain = socket;
}
public void run(){
try{
System.out.println("Accepted new connection from " + sckMain.getLocalSocketAddress());
BufferedReader inStream = new BufferedReader(new InputStreamReader(sckMain.getInputStream()));
String inputLine;
while((inputLine = inStream.readLine()) != null){
ProcessInput(inputLine);
}
CloseStreams();
}
catch(Exception e){e.printStackTrace();}
}
public void CreateOutputStream(){
try{
BufferedWriter outStream = new BufferedWriter(new OutputStreamWriter(sckMain.getOutputStream()));
}
catch(Exception e){
e.printStackTrace();
}
}
private void ProcessInput(String strData){
if(outStream == null){
System.out.println("outStream is null but why?????");
CreateOutputStream();
}
String[] cmdLine = strData.split(";");
if(cmdLine[0].equals("UserLogin")){
try{
outStream.write("Hello");
outStream.flush();
}
catch(Exception e){e.printStackTrace();}
}
else if(cmdLine[0].equals("Exit")){
CloseStreams();
}
}
private void CloseStreams(){
try{
outStream.close();
inStream.close();
sckMain.close();
}
catch(Exception e){e.printStackTrace();}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks