Reputation: 697
I have android client and C++ server with working ssl (TLS) connection. But for every message I have to open new ssl session and so on. Can I send multiple messages over one ssl socket? How? I have tried reuse my one-message working code as below.
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream()));
w.write(request, 0, request.length());
w.flush(); // this works (server got data)
BufferedReader r = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
StringBuilder response = new StringBuilder();
String actRecieved = null;
while ((actRecieved = r.readLine()) != null) {
response.append(actRecieved);
}
Log.d("TEST", "one: " + response.toString()); // this works (server send data, I read it)
request = "some special request 2";
w.write(request, 0, request.length());
w.flush(); // this does not work, no data went to server, but no error occured
actRecieved = null;
response = new StringBuilder();
while ((actRecieved = r.readLine()) != null) {
response.append(actRecieved);
}
Log.d("TEST", "two: " + response.toString()); // this does not work as well, because server is not send any data
So, where is the problem? Can anyone tell me?
Update
I have just found out that 2 write requests will reach the server when I don't read from stream.
Is there any problem with using input and output stream mutliple-times from one socket? Should I use two sockets - one for read and one for write?
Help anybody.
Upvotes: 0
Views: 694
Reputation: 697
Ok, finally I found solution on my own. The problem was in reading, because r.readLine() blocked until server closed connection, so after this nothing could be sent.
Now client is not waiting for server to close connection, but it checks if the message is complete. Like this:
while ((actRecieved = r.readLine()) != null) {
response.append(actRecieved);
if(actRecieved.endsWith("SomeEndMark")
break;
}
Hope this will help to someone else.
Upvotes: 2