• Post Reply Bookmark Topic Watch Topic
  • New Topic

IO streams / sockets

 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to write a simple piece of code that will listen on a server port to any incoming connections and redirect them to another port. E.g. listen on port 80 and redirect on port 81 of the same (or different) server.

When I listen and read the input stream, I do as follows:



However when I read the input stream, the program hangs... What is it I'm doing wrong? I was looking to develop something very simple, along the lines of TCPMon...

Thanks for the advice.
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default IO streams in Java are blocking streams. Meaning some IO related calls like serverSocket.accept(), normalSocket.read(), normalSocket.write() will block the execution untill unless the specified action can be completed.

When you try to read and if there are no bytes ready in the socket buffer to read, the call will block untill,

1. Socket closes (meaning no bytes will be every be ready)
2. Some bytes becomes available
3. Timeout occurs

Timeout interval can be specified using the overloaded method which takes number of milliseconds to one of those blocking calls.

Bottomline: Your programme is hanging due to the reason that you have not specified any timeout or the other side of the socket is not writing any bytes for you to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!