0

what it do.. 1. an iterative dictionary server that is listening clients requests.. 2. connection will be established.. 3. server will accept input string from client.. 4. then server will search meaning of string from a file.. 5. then server will return meaning to the client..

problem is with the while loop of server.. if it finds word it will send that word's meaning to client..fine.. but if word is not found... this

if(d.equals(null)){
    input="No knowledge";
    out.println(input);
    out.flush();
}

doesn't execute... client says null and server says null exception... what i am doing wrong here... i'm not getting it...!!

i have tried to changed this code... client-server online dictionary program in java

client: import java.io.; import java.net.; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner;

    public class DCC1 {

    public static void main(String[] args) throws IOException {

    final int PORT = 8888;
    Socket s = null;

    PrintWriter out = null;

    try {
        s = new Socket("localhost", PORT);
        out = new PrintWriter(s.getOutputStream()); // Output stream to the server
    }
    catch (UnknownHostException ex) {
        System.err.println("Unknown host: " + PORT);
        System.err.println("Exception: " + ex.getMessage());
        System.exit(1);
    }
    catch (IOException ex) {
        System.err.println("Cannot get I/O for " + PORT);
        System.err.println("Exception: " + ex.getMessage());
        System.exit(1);
    }

    Scanner user = new Scanner(System.in); // Scanning for user input
    System.out.print("Enter String: ");
    String input;

    input = user.next(); // Hold the input from the user
    out.println(input); // Send it to the server
    out.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream( )));
    System.out.println(br.readLine());

    out.close();
    s.close();
}    

}

server:

    import java.io.*; 
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;

    public class DSC1 
    {
     public static void main(String[] args) throws IOException 
    {

    final int PORT = 8888;
    final String FILE_NAME = "dictionary.dat";

    ServerSocket server = new ServerSocket(PORT);
    Socket s = null;
    PrintWriter out = null;
    Scanner in = null;
    FileInputStream fin = null;
    ObjectInputStream oin = null;

    while (true) 
    {
        try 
        {
            s = server.accept();
        }
        catch (IOException ex) 
        {
            System.err.println("Accept failed");
            System.err.println("Exception: " + ex.getMessage());
            System.exit(1);
        }

        System.out.println("Accepted connection from client");

        try 
        {
            in = new Scanner(s.getInputStream()); // Input stream from the client
            out = new PrintWriter(s.getOutputStream()); // Output stream to the client
            String temp = in.next(); // String holding the word sent from the client
            System.out.println("From the client " + temp);
            String input = null;

            fin = new FileInputStream(FILE_NAME);// The dictionary file
            oin = new ObjectInputStream(fin);
            dictionary d = (dictionary)oin.readObject();

            while(d!= null)
            {
             System.out.println("in loop...");              
             if(d.name.equals(temp)){

               input=d.meaning;
               d.printDic();
               out.println(input);
               out.flush();
               break;
             } 
             d = (dictionary)oin.readObject();

             if(d.equals(null))
             {
               input="No knowledge";
               out.println(input);
               out.flush();
             }
            }
        }

        catch (ClassNotFoundException|IOException ex) 
        {
            System.err.println("Exception: " + ex.getMessage());
            System.out.println("Closing connection with client");
            in.close();
            System.exit(1);
        }
        in.close();
    }
}    

}

Community
  • 1
  • 1
jakbar
  • 38
  • 4
  • If you get an exception and wonder why, then post the complete stack trace of this exception. Note that when you hit the end of an ObjectInputStream and try to read an object, you won't get null, but an exception. This is documented. – JB Nizet Nov 11 '14 at 14:43

1 Answers1

0

Don't use d.equals(null). If you want to check if d is null, just do if (d==null).

Why? There's no way this can return true, so probably that's the reason why this code never executes and you don't get what you expect.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159