Results 1 to 3 of 3
  1. #1
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Java networking, client receives null on readLine()

    Sometimes the program works fine, other times it crashes on readLine() in ChatClient, receiving null


    Thanks all.

    Java Code:
    package battleships;
    
    /* ChatClient.java */
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class ChatClient {
        private static int port = 1001; /* port to connect to */
        private static String host = "localhost"; /* host to connect to */
       String[] sArray = new String[3];
    
        
        public ChatClient (String[] toSend) throws IOException {
    
            Socket server = null;
            sArray = toSend;
            try {
                /* try to open a socket to the server at the given host:port */
                server = new Socket(host, port);
            } catch (UnknownHostException e) {
                System.err.println(e);
                System.exit(1);
            }
            
            /* obtain an output stream to the server... */
            PrintWriter out = new PrintWriter(server.getOutputStream(), true);
            /* ... and an input stream */
            BufferedReader in = new BufferedReader(new InputStreamReader(
                        server.getInputStream()));
                   
            /* loop reading messages from stdin, send them to the server 
             * and read the server's response */
            if ( (toSend == null))
            {
                out.println("load");
                
            
            sArray[0] = in.readLine();
            sArray[1] = in.readLine();
            sArray[2] = in.readLine();
            System.out.println(sArray[0] + " first string");
            }
            else {
                out.println("save");
                out.println(sArray[0]);
                out.println(sArray[1]);
                out.println(sArray[2]);
                out.flush();
                System.out.println("written array to server");
            }
        }
            public String[] getS () {
                return sArray;
            }
        }
    Java Code:
    package battleserver;
    /* ChatServer.java */
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    
    class BattleServer {
        private static int port = 1001; /* port to listen on */
        private static String[] sArray = new String[3];
        
        public static void main (String[] args) throws IOException {
    
            class ClientConn implements Runnable {
                private Socket client;
    
                ClientConn(Socket client) {
                    
                    
                    this.client = client;
                    
                }
    
                public void run() {
                    sArray = init();
                    BufferedReader in = null;
                    PrintWriter out = null;
                    try {
                        /* obtain an input stream to this client ... */
                        in = new BufferedReader(new InputStreamReader(
                                    client.getInputStream()));
                        /* ... and an output stream to the same client */
                        out = new PrintWriter(client.getOutputStream(), true);
                    } catch (IOException e) {
                        System.err.println(e);
                        return;
                    }
                    try {
                    
                    String msg = in.readLine();
                    System.out.println("msg = " + msg);
                    
                        /* loop reading messages from the client, 
                         * output to stdin and send back an "OK" back */
                    if (msg.equals("load")) {        
                    System.out.println("sArray out " + sArray[0]);
                            out.println(sArray[0]);
                            out.flush();
                            System.out.println("sArray out " + sArray[1]);
                            out.println(sArray[1]);
                            out.flush();
                   System.out.println("sArray out " + sArray[2]);
                            out.println(sArray[2]);
                            out.flush();
                    }
                    else {
                        sArray[0] = in.readLine();
                        sArray[1] = in.readLine();
                        sArray[2] = in.readLine();
                        System.out.println("saved " + sArray[0]);
                    }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    
                }
            }
    
            ServerSocket server = null;
            try {
                server = new ServerSocket(port); /* start listening on the port */
            } catch (IOException e) {
                System.err.println("Could not listen on port: " + port);
                System.err.println(e);
                System.exit(1);
            }
    
            Socket client = null;
            while(true) {
                try {
                    client = server.accept();
                } catch (IOException e) {
                    System.err.println("Accept failed.");
                    System.err.println(e);
                    System.exit(1);
                }
                /* start a new thread to handle this client */
                Thread t = new Thread(new ClientConn(client));
                t.start();
            }
        }
    
    
        static public String[] init() {
            int x[] = new int[4];
            int y[] = new int[4];
            String sArray[] = new String[3];
            String s = "";
            int z = 0;
    
            for (int k = 0; k < 3; k++) {
                int startx = (int) (Math.random() * 7);
                int starty = (int) (Math.random() * 7);
                int length = (int) (Math.random() * 7);
                int direction = (int) (Math.random() * 2);
    
                switch (direction) {
                    case 0:
                        for (int j = 0; j <= 3; j++) {
                            y[j] = starty;
                            x[j] = startx + j;
                        }
                        break;
                    case 1:
                        for (int j = 0; j <= 3; j++) {
                            x[j] = startx;
                            y[j] = starty + j;
                        }
                        break;
                    default:
                        System.out.println("error");
                }
                for (int i = 0; i < x.length; i++) {
                    s = s + Integer.toString(x[i]);
                }
                s = s + " ";
                for (int i = 0; i < y.length; i++) {
                    s = s + Integer.toString(y[i]);
                }
                sArray[z] = s;
                z++;
    
                x = new int[4];
                y = new int[4];
                s = "";
            }
    
            return sArray;
    
        }
    }
    Last edited by space.puffin; 09-27-2011 at 08:18 PM.

  2. #2
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Java networking, client receives null on readLine()

    in.ready returns false, so in cannot be read, any ideas folks?
    have seperated InputStreamReader from BufferedReader.
    Java Code:
    InputStreamReader isr = new InputStreamReader(server.getInputStream());
            System.out.println("isr = " + isr.ready());
            BufferedReader in = new BufferedReader(isr);
            System.out.println("in = " + in.ready());
    isr = false
    in = false.

    Thanks in advance.
    Last edited by space.puffin; 09-27-2011 at 08:43 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Java networking, client receives null on readLine()

    readLine() in ChatClient, receiving null
    Have you read the API doc for the readLine() method?
    Can it return null? Is that expected or not?
    You should check after every read to see if you have read a null.

Similar Threads

  1. Replies: 1
    Last Post: 10-28-2009, 02:39 AM
  2. Networking in Java
    By thangavel14 in forum Networking
    Replies: 3
    Last Post: 07-30-2009, 01:47 AM
  3. Java networking.
    By tim in forum New To Java
    Replies: 7
    Last Post: 07-16-2009, 10:43 PM
  4. Replies: 6
    Last Post: 10-23-2008, 11:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •