Grant
Grant

Reputation: 11356

Unable to send data over TCP from java socket to .net tcp server

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();

Upvotes: 2

Views: 2261

Answers (3)

user207421
user207421

Reputation: 310850

Close the PrintWriter, not the Socket.

Upvotes: 1

Jim
Jim

Reputation: 3284

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

Upvotes: 0

bcat
bcat

Reputation: 8941

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.

Upvotes: 2

Related Questions