File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "SocketException: socket closed ???" Watch "SocketException: socket closed ???" New topic
Author

SocketException: socket closed ???

RemmedOut
Greenhorn

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 java.io.*; import java.net.*;
public class weird{
public static void main(String args[]){
try{
// Open a UDP socket on port 2000
// and set he receive timeout
DatagramSocket sock =
new DatagramSocket(2000);
sock.setSoTimeout(100);
// 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 ???
sock.close();
} catch(Exception e){System.out.print(e);}
}
}
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
RemmedOut,
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SocketException: socket closed ???