File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in Socket Server

 
Siamak Saarmann
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to migrate my old unix BSD-Sockets software to java. I am trying to practice sockets in my simple HelloServer but I have this problem:

Server gives an exception after responding to first client and does not wait for next client.

D:\JavaProg\HelloClientServer\bin>java HelloServer
Exception in thread "main" java.lang.NullPointerException
at HelloServer.main(HelloServer.java:26)

while client does not have any problem:

D:\JavaProg\HelloClientServer\bin>java HelloClient localhost
Client sending : Hello, I am Siamak
Server response: Good morning, Siamak

Could someone please advise?

Regards,
mac






 
Kedar Bhawarthi
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just replace content of you HelloServer jar file with this:


your earlier server was not multi threaded and it was throwing exception from
ir.readLine();
as client was closing its output stream with
os.close();
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You show us the exception, which is nice, but you don't tell us which line of your code is throwing the null pointer exception, which is not nice.
I suspect it is this line:

BufferedReader.readLine() returns null when it reaches EOF.
Have a look at Java Tutorial: All About Sockets for a good introduction of Java Sockets.
 
Siamak Saarmann
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People, thank you. The problem was that, I was using a closed client sockets (after it was accepted, responded and closed). If I want to have a single thread server then below changed code works but as it can only serve one connection at a time it will be wise to use Kedar's multi thread solution.

Thank you Kedar, thank you Joe.


 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic