This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Exception in Socket Server" Watch "Exception in Socket Server" New topic
Author

Exception in Socket Server

Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
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








PhD Candidate: Distributed and Parallel Systems, Simulation and Modeling
Kedar Bhawarthi
Greenhorn

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
ir.readLine();
as client was closing its output stream with
os.close();
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Siamak Saarmann
Ranch Hand

Joined: Aug 21, 2004
Posts: 77
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
 
subject: Exception in Socket Server
 
Similar Threads
JAVA SOCKET PROGRAMMING
Client sends just 1 character to server (socket reset)
closing thread and its childs
Address already used problem in Java Socket application
simple socket programming