2

i am trying to get an android app to send tcp data to a server on my network. The server was written in c#.

When i use the code below to transmit data, the server only receives a whole series of \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.

Does anyone know why?

Socket socket = new Socket(host, port);
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
pw.print("test");
socket.close();
1
  • 1
    One thought is to check if the encoding of what is being sent matches what the server expects. Commented Nov 2, 2010 at 5:47

3 Answers 3

2

I'm not sure if this has anything to do with it, but you might want to try manually flushing the PrintWriter. You did pass true to its constructor to request autoflushing, but I believe this only occurs when you print a newline character, which your sample code doesn't do.

Sign up to request clarification or add additional context in comments.

2 Comments

shouldn't the close flush before closing?
Yes, but that will only flush the socket's buffers. The PrintWriter class maintains buffers of its own which must be flushed before the socket is closed to ensure that all data is delivered properly.
1

Close the PrintWriter, not the Socket.

Comments

0

you may want to try println("test"). In java you need to flush the line, so that may be the cause of your issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.