Results 1 to 3 of 3
- 09-27-2011, 07:49 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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.
- 09-27-2011, 08:40 PM #2
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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.
isr = falseJava Code:InputStreamReader isr = new InputStreamReader(server.getInputStream()); System.out.println("isr = " + isr.ready()); BufferedReader in = new BufferedReader(isr); System.out.println("in = " + in.ready());
in = false.
Thanks in advance.Last edited by space.puffin; 09-27-2011 at 08:43 PM.
- 09-27-2011, 09:00 PM #3
Similar Threads
-
Socket Exceptions not being generated, readLine not returning null
By jmlsteele in forum NetworkingReplies: 1Last Post: 10-28-2009, 02:39 AM -
Networking in Java
By thangavel14 in forum NetworkingReplies: 3Last Post: 07-30-2009, 01:47 AM -
Java networking.
By tim in forum New To JavaReplies: 7Last Post: 07-16-2009, 10:43 PM -
Pretty awesome - James Gosling receives Sexy Programmer Title
By tim marcus in forum EntertainmentReplies: 6Last Post: 10-23-2008, 11:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks