Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java Chapter 15 Questiion

 
Michael Hall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In chapter 15 of Head First Java I want to try out the SimpleChatClient and VerySimpleChatServer programs but I only have one computer. Is there a way I can set up a situation that passes as a server and a terminal on my desktop computer so I can run those programs in chapter 15? - Thanks
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use your desktop as sever and client.
 
Steve Gibson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you notice in all their client/server examples the remote IP address is 127.0.0.1 which is yout local loopback (aka localhost) ip address. So what your really are doing is an interprocess call.

Be aware that the example in chap18 ServiceServer does not work if you seperate out the class's into seperate directories. It only works if the service class's are in the same directory as the client. The code does not send the service's to the client as indicated in the sample text. I emailed Bert Bates about it, hopefully it will make the errata for the book.
 
Michael Hall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 18? ServiceServer?
In the Head First Java book the Chat Client I am talking about is in Chapter 15 (at least in my version) and consists of VerySimpleChatServer and SimpleChatClient. Anyway when I run the server in one command prompt window and the client in another window and type in text in the client and send it I get the following errors:

java.lang.NullPointerException
at VerySimpleChatServer$ClientHandler.run(VerySimpleChatServer.java:19)
at java.lang.Thread.run(Unknown Source)

Here is code:


Line 19 is - while((message=reader.readLine()) != null){

Help - No Clue.
 
Steve Gibson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hall:
Chapter 18? ServiceServer?


2 things...

1.My chapter 18 referece was just letting you know that the example really dosen't work as advertised.

2. In your ClientHandler class code for the chat server, you never actually created (instantiated) the BufferedReader (the missing code is in bold/italics below). :

public ClientHandler(Socket clientSocket){
try{
sock=clientSocket;
InputStreamReader isReader=new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch(Exception ex) {ex.printStackTrace();}
}


So in your original code the line:

while((message=reader.readLine()) != null)

reader was pointing to (referencing) nothing (null), because reader had never been instantiated (created).




[ March 01, 2006: Message edited by: Steve Gibson ]
[ March 01, 2006: Message edited by: Steve Gibson ]
 
Michael Hall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, That did the Trick. It works fine. Thanks a lot.
I assume since you are a greenhorn that you are not THE Steve Gibson.
 
Steve Gibson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hall:
Well, That did the Trick. It works fine. Thanks a lot.
I assume since you are a greenhorn that you are not THE Steve Gibson.


I'm not sure who THE Steve Gibson is..... the only one I can think of is the SpinRite Steve Gibson, in which case I'm not :-)

I'm only a greenhorn on this board, I've been coding since 1987 in RPGIII, RPGIV, ILE-RPG, C (win sdk), VB (all versions) then did a long stint in management. Now I'm back Coding in C# and Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic