Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

End of stream.  RSS feed

 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a string using DataOutStream classes writeBytes method.
os.writeBytes(m_send_buffer);
On the other side I am reading using InputStream in while loop.
I am using following code on the other side(proxy server)
while((bytes_read=from_client.read(buffer))!=-1) {
to_server.write(buffer, 0, bytes_read);
to_server.flush();
}

I am gettinng blocked in the loop,I guess the reason is I am not getting end of stream.Can anyone suggest an alternate or am I doing something wrong?.
Thank!!
Arvind

}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you close the original DataOutputStream? If it's still open, then the file may not have an end yet, so the InputStream just keeps waiting for more bytes to be read.
 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!.I noticed that.I tried closing the stream,which seems to close the underlying Socket.In latter part of my program I need to get an inputstream on the same socket to read the response.So when I try to get Inputstream on the socket I get SocketClosed error
java.io.IOException: Socket Closed
at java.net.PlainSocketImpl.getInputStream(PlainSocketImpl.java:421)
at java.net.Socket$1.run(Socket.java:335)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getInputStream(Socket.java:332)
at com.sungard.omniconnect.framework.TransIp.receiveData(TransIp.java:21
6)
Other work around would be to send a flag to indicate the end of message, but this will remove the generic nature of my application.So of no use.Any other better solutions please!!
Thanks!!
Arvind
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense - if you want to keep using the same socket, you can't close it. (Though it would be nice if the documentation made it clear that closing a socket's OutputStream also closed the socket itself.) I suppose you could just open a new Socket later on the same port - but that may well take more resources than you want. If you want to keep the socket open, you need to establish some protocol between the input and output streams . I'd probably send a control-D character (Unicode value 4, End-of-transmission). When you read that, you know the current message is ended. That may "remove the generic nature of the application" - but I'm not sure what you mean by that anyway. Are other applications going to be trying to communicate on the same port? Is it unreasonable to expect them to follow the same protocol?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!