File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes SocketException: socket closed ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "SocketException: socket closed ???" Watch "SocketException: socket closed ???" New topic

SocketException: socket closed ???


Joined: Mar 12, 2002
Posts: 1
I have been trying to code a client and a server program that use the UDP protocol, but I continually encounter this weird problem (sporatically) and it halts my programming when I do. Lately, I haven't been able to make it go away and it's taking up so much of my time I thought I'd post about it here, since it's probably caused by a common, stupid mistake on my part. Basically, the problem is sometimes a call to DatagramSocket.receive(..) will throw a SocketException with message: "socket closed." I have narrowed the problem down far enough that I think it has something to do with previous calls to DatagramSocket.send(..) not reaching their destination and plugging up the socket's stream, but this is just a guess. I wrote the following simple program that demonstrates how receive always seems to fail (always throws the exception) right after a send. Any help would be appreciated regarding this problem. Thanks.
// Program Code:
import*; import*;
public class weird{
public static void main(String args[]){
// Open a UDP socket on port 2000
// and set he receive timeout
DatagramSocket sock =
new DatagramSocket(2000);
// Create,send,&receive some packets
byte[] b = new byte[4];
DatagramPacket sendPacket =
new DatagramPacket(b,b.length,
InetAddress.getLocalHost(), 2001);
DatagramPacket receivePacket =
new DatagramPacket(b,b.length);
// This receive times out
// (expected operation)
System.out.print("\nRECEIVE ATTEMPT ");
try { sock.receive(receivePacket); }
catch(Exception e){System.out.print(e);}
// This send works (expected operation)
System.out.print("\nSEND ATTEMPT ");
try { sock.send(sendPacket); }
catch(Exception e){System.out.print(e);}
// BUT...This receive fails with
// "socket closed" exception
System.out.print("\nRECEIVE ATTEMPT ");
try { sock.receive(receivePacket); }
catch(Exception e){System.out.print(e);}
// YET...This receive times out like before
// (expected operation)
System.out.print("\nRECEIVE ATTEMPT ");
try { sock.receive(receivePacket); }
catch(Exception e) {System.out.print(e);}
// QUESTION: Why does the first receive
// attempt made after sending to a socket
// always generate a "socket closed"
// exception when subsequent receives
// seem to work fine ???
// I realize the sends here most definitely
// aren't being received by the localhost
// on port 2001 (as they're directed at).
// When I run an acceptor program that
// listen and accepts the sends on port
// 2001, the receive calls here don't
// throw the socket exception...But can
// someone explain why it makes a
// difference if the send packets are
// picked up or not? I thought only
// the receive method puts a "block" on
// the stream/program ???
} catch(Exception e){System.out.print(e);}
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
Javaranch has a naming policy. Please re-register with a name that complies with that policy so that you may continue to post here. Thanks!
Joe McGuire
Sun Certified Java™ 2 Programmer, BEA WLS Certified Developer
I agree. Here's the link:
subject: SocketException: socket closed ???
It's not a secret anymore!