Sending arbitrarily long string over Java TCP socket
Posted
by
bibismcbryde
on Stack Overflow
See other posts from Stack Overflow
or by bibismcbryde
Published on 2012-11-10T04:14:37Z
Indexed on
2012/11/10
5:00 UTC
Read the original article
Hit count: 110
I have an Android app that communicates over a TCP socket with a server I wrote. The method I'm using now to read and write output works fine for smaller strings (up to 60kB) but I get an exception thrown when the string is much longer than that. Here is the relevant part of what I have for the server and client:
Server:
DataInputStream dis = null;
DataOutputStream dos = null;
try {
dis = new DataInputStream(server.getInputStream());
dos = new DataOutputStream(server.getOutputStream());
String input = "";
input = dis.readUTF();
handle_input info = new handle_input(input, id);
String xml = info.handle();
dos.writeUTF(xml);
server.close();
}
Client:
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
Boolean result;
try {
socket = new Socket(ip, port);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
dos.writeUTF(the_text);
String in = "";
while (in.equals("")) {
in += dis.readUTF();
}
}
How can I modify it to deal with potentially enormous Strings? I've been looking around and can't seem to find a clear answer.
Thanks.
© Stack Overflow or respective owner