Java Tcp socket sending extra data?

I have a class that will send data in java, the server requires a certain string to actually work.

I am sending the correct string, this is the connection and sending process:

   // CONNECT
    try {

        // GET SERVER IP
        InetAddress serverAddr = InetAddress.getByName(this.terminalIp);

        // CREATE SOCKET
        Socket hcmSocket = new Socket(serverAddr,this.terminalPort);

        PrintWriter sendingStream = new PrintWriter(new BufferedWriter(new OutputStreamWriter(hcmSocket.getOutputStream())), true);
        BufferedReader recivingStream = new BufferedReader(new InputStreamReader(hcmSocket.getInputStream()));

        // SEND REQUEST
        sendingStream.println("00 0012552 003365 004152");

The server rejects this even though it is correct.

When I trace the connection with wireshark I see that the hex being sent is adding on an extra 0x000a which is a Line Feed.

My problem is that I don't want that to be sent, I am making sure that the string I am sending it has no extra white space at the end to create this.

This is the Hex I wan to send :30 30 1c 30 30 31 31 30 30 30 1c 30 30 33 32 35 32 1c 30 30 34 31 32 35 31

But what is acutally being sent is: 30 30 1c 30 30 31 31 30 30 30 1c 30 30 33 32 35 32 1c 30 30 34 31 32 35 31 0a

Could it be the sendingStream that is automatically adding this in?

Jon Skeet
people
quotationmark

My problem is that I don't want that to be sent, I am making sure that the string I am sending it has no extra white space at the end to create this.

No, you're not.

Look at your code:

sendingStream.println("00 0012552 003365 004152");

From the docs of PrintWriter.println(String):

Prints a String and then terminates the line. This method behaves as though it invokes print(String) and then println().

That's why you're getting the line terminator - you asked for it.

If you don't want it, just call print instead of println. Ideally, get rid of the PrintWriter entirely as it will hide exceptions from you. Just use the BufferedWriter instead. (I'd personally specify the charset explicitly, even though I know it's reasonable to expect the default to be UTF-8 on Android...)

people

See more on this question at Stackoverflow