• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OutputStream Problem in Socket

 
reddy sridhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I created a socket connection to an application and before sending data to the other side of application , I made the my application to sleep for 10 seconds and mean while I closed the other application . It leads to the socket unavailable . But In my application the next line after Thread.sleep is sck.isConnected() prints me true and If I create OutputStream and try to write some bytes , writing data to the outputstream is success. How is it possible? Here is the piece of code I used

Socket sck= new Socket();

SocketAddress intAdd=new InetSocketAddress(ipAddress,portNo)) ;
sck.connect(intAdd,20000);
Thread.sleep(10000);
System.out.println("Is Socket Connection Available: "+sck.isConnected());
OutputStream sockOut= sck.getOutputStream(); // Either Here Exception Should Throw
sockOut.write(data,0,data.length); // Or Writing Data to Output Stream should fail
sockOut.flush();

Thanks & Regards
Sridhar. R

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4598
303
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reddy sridhar wrote:I closed the other application


How exactly did you close it? Depending on the type of the application (swing, web, mobile, etc.) there are ways to execute some code before the application is terminated. You should write a code in a such a way that explicitly closes the socket right before the application is closed. Assuming your application is a console-based or swing-based, what all you need is a shutdown hook that closes all open connections.
 
reddy sridhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:
reddy sridhar wrote:I closed the other application


How exactly did you close it? Depending on the type of the application (swing, web, mobile, etc.) there are ways to execute some code before the application is terminated. You should write a code in a such a way that explicitly closes the socket right before the application is closed. Assuming your application is a console-based or swing-based, what all you need is a shutdown hook that closes all open connections.


Its not a java application where i am sending the data or connected to . Its just UI developed in other language . I just telnet the application where it is listening , and close the application , the telnet command exit . If he dint shutdown the sockets available before closing the application , the above experiment should fail .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic