This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

SocketException: socket closed ???

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);}
}
}
 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic