RemmedOut

Greenhorn
+ Follow
since Mar 12, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by RemmedOut

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);}
}
}