[英]WriteUTF and ReadUTF, how can i write a string and get a short exception in another side?
Basically i'm doing a socket connection between two sides. 基本上我正在做两侧之间的套接字连接。 A Java server and a Android client.
Java服务器和Android客户端。 I'm converting objects to String Json using Gson library , and sending them through method WriteUTF from Android client.
我正在使用Gson库将对象转换为String Json,并通过Android客户端通过WriteUTF方法发送它们。 But when i try to get response in Java server i get a "java.io.EOFException at java.io.DataInputStream.readUnsignedShort(Unknown Source)".
但是,当我尝试在Java服务器中获取响应时,得到了“ java.io.DataInputStream.readUnsignedShort(Unknown Source)处的java.io.EOFException”。 How, if i didn't used this method (readUnsignedShort) or even didn't send any Short ?
如果我没有使用此方法(readUnsignedShort)甚至没有发送任何Short怎么办? Here comes the codes.
代码来了。
Writing from Android client: 从Android客户端编写:
try {
DataInputStream in = new DataInputStream (s3.getInputStream());
DataOutputStream out = new DataOutputStream(s3.getOutputStream());
ap = (ArrayList<Produto>) InternalStorage.readObject(getBaseContext(),"pedido");
for(int i=0;i<ap.size()&& ap.size()>0;i++){
Produto p = new Produto("", "", 0f, "", "", 0);
p = ap.get(i);
Gson gson = new Gson();
String json = gson.toJson(p);
out.writeUTF(json);
out.flush();
}
And Reading from Java server: 并从Java服务器读取:
try {
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
while((line = in.readUTF()) != null) {
boolean error = false;
String json = line;
Gson gson = new Gson();
Produto p2 = gson.fromJson(json, Produto.class);
c = dbDelegate.getCommandByNumber(p2.getMesa());
p = dbDelegate.getProductByName(p2.getNome());
if(c!=null && p!=null){
error = false;
inCommand.setCommand(c);
inCommand.setDateAndTime(new Timestamp(System.currentTimeMillis()));
inCommand.setProduct(p);
inCommand.setLogin(FishingBoardFrameCashRegister.getInstance().getLogin());
inCommand.setQuantity(p2.getQuantidade());
inCommand.setPrice(String.valueOf((p2.getValor()*p2.getQuantidade())));
inCommand.setObs(p2.getObs());
inCommand.setProductName(p.getName());
inCommand.setType(Product.ALL);
inCommand.setSession(0);
inCommand.setCommandSession(c.getSession());
if(dbDelegate.insertProductInCommand(inCommand) == DbDelegate.CASH_CLOSED){
error = true;
}
FishingBoardFrameCashRegister.getInstance().updateTable();
}else{
error = true;
}
The log may help too: 日志可能也有帮助:
trueIOException on socket listen: java.io.EOFException
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at com.keymax.server.doComms3.run(Server3.java:71)
Server3.java:71 is this line in server code: while((line = in.readUTF()) != null) { Server3.java:71是服务器代码中的这一行:while((line = in.readUTF())!= null){
I think the EOFException
is expected here, because that's what you will get if you call readUTF
and there is no data left. 我认为这里会出现
EOFException
,因为如果您调用readUTF
并且没有剩余数据,这将是您得到的。
I would fix this by using writeInt
before the loop to write out how many strings are about to be written, and then readInt
on the other end, so that you don't call readUTF
more times than the number of strings actually available in the stream. 我将通过在循环之前使用
writeInt
来写出将要写入多少个字符串,然后在另一端使用readInt
解决此问题,以使您调用readUTF
次数不会超过流中实际可用的字符串数。
writeUTF
probably writes the length of the string and then the data in the string, which explains why readUnsignedShort
is the one getting the EOFException
. writeUTF
可能先写字符串的长度,然后写字符串中的数据,这解释了为什么readUnsignedShort
是获得EOFException
那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.