2 qq 26556381 qq_26556381 于 2016.03.17 19:43 提问

java的socket编程问题:为什么客户端接收服务器的数据时会有空白数据项?

例如:03-17 07:02:00.330 1984-2007/? I/info﹕ client buff --> 0/20160316//早餐/123.0
03-17 07:02:00.340 1984-2007/? I/info﹕ insert expend_table
03-17 07:02:00.340 1984-2007/? I/info﹕ client buff -->
03-17 07:02:00.340 1984-2007/? I/info﹕ client buff --> 0/20160316//午餐/22.0
03-17 07:02:00.340 1984-2007/? I/info﹕ insert expend_table
03-17 07:02:00.340 1984-2007/? I/info﹕ client buff -->
03-17 07:02:00.340 1984-2007/? I/info﹕ client buff --> 0/20160316/22ss/服装/123.0

数据内容忽视。client buff是客户端接受到的数据
就是为什么会是一条有数据一条没有数据这样循环下去?
服务端代码:
pw = new PrintWriter(s.getOutputStream());
Cursor c = dbManager.query(Constant.DATA_TABLE,"username=?",new String[]{username});
while (c.moveToNext()){
// client端发送数据的顺序: isIncome + "/" + date + "/" + title + "/" + content + "/" + money
String title = c.getString(c.getColumnIndex("title"));
String money = c.getString(c.getColumnIndex("money"));
Long date = c.getLong(c.getColumnIndex("date"));
String isIncome = c.getString(c.getColumnIndex("isIncome"));
String content = c.getString(c.getColumnIndex("content"));
String data = isIncome + "/" + date + "/" + title + "/" +
content + "/" + money + '\n';
pw.println(data);
pw.flush();
}
String endFlag = username + "/end" + '\n';
pw.println(endFlag);
pw.flush();

客户端代码:
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String buff;
while (!(buff = br.readLine()).endsWith(mUsername + "/end")) {
// server端发送数据的顺序: isIncome + "/" + date + "/" + title + "/" + content + "/" + money
String[] str = buff.split("/");
if (str.length == 5) { //确保接收的数据正确
ContentValues values = new ContentValues();
values.put("isIncome", str[0]);
values.put("date", Long.valueOf(str[1]));
values.put("title", str[2]);
values.put("content", str[3]);
values.put("money", str[4]);
if ("0".equals(str[0])) {
if (!dbManager.insert(Constant.EXPEND_TABLE, values)) {
flag = false;
break;
}
} else if ("1".equals(str[0])) {
if (!dbManager.insert(Constant.INCOME_TABLE, values)) {
flag = false;
break;
}
}
}
}
db.setTransactionSuccessful();
db.endTransaction();

3个回答

jpbirdy
jpbirdy   2016.03.17 20:48

不是代码的问题,就可能是原生socket的粘包问题。

qq_26556381
qq_26556381 代码贴上去了,帮忙看下呗,谢谢。 也不懂什么时粘包问题
一年多之前 回复
wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.03.17 21:11
 那要看你服务器端处理响应代码,以及客户端接收响应数据代码的处理是否有不当之处了。
qq_26556381
qq_26556381 代码贴上去了,帮忙看下呗,谢谢
一年多之前 回复
qq_26556381
qq_26556381   2016.03.18 20:29

服务端代码:
pw = new PrintWriter(s.getOutputStream());
Cursor c = dbManager.query(Constant.DATA_TABLE,"username=?",new String[]{username});
while (c.moveToNext()){
// client端发送数据的顺序: isIncome + "/" + date + "/" + title + "/" + content + "/" + money
String title = c.getString(c.getColumnIndex("title"));
String money = c.getString(c.getColumnIndex("money"));
Long date = c.getLong(c.getColumnIndex("date"));
String isIncome = c.getString(c.getColumnIndex("isIncome"));
String content = c.getString(c.getColumnIndex("content"));
String data = isIncome + "/" + date + "/" + title + "/" +
content + "/" + money + '\n';
pw.println(data);
pw.flush();
}
String endFlag = username + "/end" + '\n';
pw.println(endFlag);
pw.flush();

客户端代码:
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String buff;
while (!(buff = br.readLine()).endsWith(mUsername + "/end")) {
// server端发送数据的顺序: isIncome + "/" + date + "/" + title + "/" + content + "/" + money
String[] str = buff.split("/");
if (str.length == 5) { //确保接收的数据正确
ContentValues values = new ContentValues();
values.put("isIncome", str[0]);
values.put("date", Long.valueOf(str[1]));
values.put("title", str[2]);
values.put("content", str[3]);
values.put("money", str[4]);
if ("0".equals(str[0])) {
if (!dbManager.insert(Constant.EXPEND_TABLE, values)) {
flag = false;
break;
}
} else if ("1".equals(str[0])) {
if (!dbManager.insert(Constant.INCOME_TABLE, values)) {
flag = false;
break;
}
}
}
}
db.setTransactionSuccessful();
db.endTransaction();

Csdn user default icon
上传中...
上传图片
插入图片