DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Angry 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();}
    }
    }

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    this has already been answered in the other Java forum at
    http://forums.devx.com/showthread.php?t=141957
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links