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
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply 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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2729
    
    3

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 (Download) - the free mock exam kit for SCJP / OCPJP
HELP me! -- Home Page -- Twitter Profile -- JavaRanch FAQ -- 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 .
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: OutputStream Problem in Socket
 
Similar Threads
only one side??
Configuring Servers or have to use existing or have to make a new?
socket acting like "canal"-one side flow only??
JUnit and Sockets
sockets in basic chat application creating trouble