aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes OutputStream Problem in Socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "OutputStream Problem in Socket " Watch "OutputStream Problem in Socket " New topic
Author

OutputStream Problem in Socket

reddy sridhar
Greenhorn

Joined: Oct 10, 2012
Posts: 2
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3168
    
  47

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.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
reddy sridhar
Greenhorn

Joined: Oct 10, 2012
Posts: 2
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 .
 
Consider Paul's rocket mass heater.
 
subject: OutputStream Problem in Socket