SSL and line break "\n"

Hey,

I have a SSL client/server pair that pass messages between each other. Basically I'm having a problem where any sent message containing a line break are split based on this line break.

So if I sent "cheese \n monster \n", the server would recieve it as two different commands ("cheese" and "monster), instead of a single string.

Is this unavoidable? I could replace the line breaks with some other identifier and replace them with line breaks when the message is recieved. But I'd rather try and find a way around this first.

Thanks in advance :)

[584 byte] By [ashdiusahd] at [2007-9-30 18:39:41]
# 1

This has nothing at all to do with SSL. Sounds like you're using BufferedReader.readLine() somewhere, and making bad assumptions about your data.

When you're talking across a Socket, you should be sending your data as a message, with a well-defined start, stop, and content. Teach your write-code to write entire messages, and your read-code to read entire messages, to clear up problems like this.

Grant

ggainey at 2007-7-6 20:53:53 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2

I wouldn't have thought so either, but in the beginning I wrote this using normal Sockets/ServerSockets and it had no problem sending messages with line breaks.

So you're saying I should have the server/client instead of real "lines" as a command have a command start signal/text and command end?

ashdiusahd at 2007-7-6 20:53:53 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3

SSL doesn't do anything with line-breaks. Once the SSL handshake has taken place, it's just a Socket. I've used JSSE to transfer all kinds of data, and it doesn't do anything to the data being sent. If your code's behavior has changed after switching to SSLSocket, then I'm quite certain you've changed more than just the socket-type along the way.

What I'm saying is, if you're using readLine() to read from the Socket stream, then it's going to give you each line in turn - that's what readLine() does, whether your end datasource is a SocketInputStream or a File. If your data has linebreaks in it, then you have two choices - don't use readLine() the way you are, or encapsulate your data as a message. Say, have a "MESSAGE START" at the beginning, and a protocol that says "do readLine() until you see 'MESSAGE END' ", for example.

Grant

(Well, there is a third option - have a protocol that breaks in the face of line-ends in the data. But I don't think you want that one...)

ggainey at 2007-7-6 20:53:53 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4
Ahh, you're completely right. My old "normal socket" tests didn't include the line breaks like I thought I had. Which is what was getting me so confused.Thanks a bunch!
ashdiusahd at 2007-7-6 20:53:53 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...