SSL reading problem in server-side
Hi guys,
I have a problem in my server implemetation with SSL Server Socket. I have created a server socket with a specfic port and bind address. Whenever a client connecfed, i grap its inputstream and starts to read as bytes. There is no problem to open server socket and certifacate authorization, and also a client successfully connects to server. But when client writes some data to its connected socket, server cannot read anything. Server throws no exception and there is no problem in writing. But the available bytes in inputstream is always 0. When i replace SSL socket with normal socket, everything is ok, server can read everything. I confused very much. since i have no concrete exception and stack trace, I know it is hard to explain and get help about my problem. I have added some parts from my code.
Could you make any suggestions?
Listening and connection part
ServerSocketFactory socketFactory = SSLServerSocketFactory.getDefault();
socket = socketFactory.createServerSocket(port,backLog,bindAddress);
Socket clientSocket = socket.accept();
in =new BufferedInputStream(clientSocket .getInputStream());
Reading part
while (continueRunning){
try{
Thread.sleep(1);
if(in.available()<1){
System.err.println(in.available());
continue;
}
MessageDecoder decoder=new MessageDecoder();
Message msg = decoder.decode(in);
if(msg ==null){
System.out.println("Decoded message is null");
continue;
}
handler.messageReceived(msg);
}catch (IOException e){
e.printStackTrace();
continueRunning=false;
try{
clientSocket.close();
}catch (IOException e1){
e1.printStackTrace();
}
}catch (InterruptedException e){
continueRunning=false;
e.printStackTrace();
try{
clientSocket.close();
}catch (IOException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
> Thread.sleep(1);
> if(in.available()<1){
> System.err.println(in.available());
> continue;
> }
Your problem is that available() always returns zero for an SSLSocket, but in any case this piece of code is accomplishing precisely nothing except literally wasting time. Delete it.
ejpa at 2007-7-13 22:40:44 >

I process bytes whenever they are available in the stream, thus i use available() for checking wheter there is any bytes to decode. The problem is there is no data in the stream althogh client seems write some data.
I discover the debugging utilities of JSSE and make some debugs. I find that client is blocked on its socket when it tries to write stream. I am not using nio, so my sockets are blocking but i cannot find any reasonable explanation for this SSL write blocking on socket. When i change my implementation and used non-SSL socket, everything is ok and there is no blocking.
Is there anyone who knows something about some kind of SSL blocking?
> I process bytes whenever they are available in the
> stream, thus i use available() for checking wheter
> there is any bytes to decode.
You are looping and sleeping and calling available(). What's the point? As you have nothing else to do in the loop except sleep according to the above code, the whole sleep/available business is still a waste of time. Why not just read()? You are also burning a lot of CPU cycles for nothing.
> The problem is there is no data in the stream althogh client seems write some data.
The problem is that regardless of whether there is data in the stream or not, SSLSocket.getInputStream().available() always returns zero. It always does this, and so you cannot use it for the purpose you intend.
This is no loss, as the purpose you intend adds no value to just doing a read(). Try it and see.
> I discover the debugging utilities of JSSE and make
> some debugs. I find that client is blocked on its
> socket when it tries to write stream. I am not using
> nio, so my sockets are blocking but i cannot find any
> reasonable explanation for this SSL write blocking on
> socket.
The 'reasonable explanation' is that the peer is never reading, so its socket receive buffer is full, so the writer's send buffer eventually fills too, at which point the writer is blocked.
> When i change my implementation and used
> non-SSL socket, everything is ok and there is no
> blocking.
That's because Socket.getInputStream().available() returns positive numbers whereas SSLSocket.getInputStream.available() always returns zero.
> Is there anyone who knows something about some kind
> of SSL blocking?
There is.
ejpa at 2007-7-13 22:40:44 >

> You are looping and sleeping and calling available().
> What's the point? As you have nothing else to do in
> the loop except sleep according to the above code,
> the whole sleep/available business is still a waste
> of time. Why not just read()? You are also burning a
> lot of CPU cycles for nothing.
>
I'll take a stab at this one.
I do something similar on the client side, checking available() in a loop; in fact, searching for the same SSL problem is what brought me here.
My loop isn't infinite, but is user-settable for the number of tries and the amount of time to try. This allows for a timeout, and the Thread.sleep call allows for the thread to be interrupted, in case another thread in the program decides it's time to shut down the app, or if it's being run from the command line then ctrl-c or kill -2 will work.
The problem with a straight read is that it's usually a blocking call and while you're in it the thread won't be interrupted, so these loops etc. are necessary.
I'm going to try variations on the proposed solution here
http://forum.java.sun.com/thread.jspa?forumID=2&threadID=392157
OK but let me state again that SSLSocket.getInputStream().available() will never return anything except zero. The last poster in that thread is wrong about the timeout closing the socket: it doesn't.
ejpa at 2007-7-13 22:40:44 >
