This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Exception in Socket Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Exception in Socket Server" Watch "Exception in Socket Server" New topic

Exception in Socket Server

Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 78

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(

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?


PhD Artificial Intelligence, OCJP1.6
Kedar Bhawarthi

Joined: May 14, 2004
Posts: 8
just replace content of you HelloServer jar file with this:

your earlier server was not multi threaded and it was throwing exception from
as client was closing its output stream with
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

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.

[How To Ask Questions On JavaRanch]
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 78
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:
subject: Exception in Socket Server
It's not a secret anymore!