java里客户端服务端发送接收怎么保持一致性?

我程序里两个端之间传送信息都是这么写的,Sockets=newSocket("127.0.0.1",8888);DataInputStreamdis=newDataInp... 我程序里两个端之间传送信息都是这么写的,
Socket s = new Socket("127.0.0.1", 8888);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos =new DataOutputStream(s.getOutputStream());
然后就用
dos.writeUTF(str);发信息和dis.readUTF()接收信息。
比如现在客户端输入用户名和密码,点击登录,客户端程序通过两个dos.writeUTF(str)分别按次序的把用户名和密码发到服务端,然后服务端通过两个dis.readUTF()分别按次序接收用户名和密码,然后服务端将接收到的用户名和密码送到数据库校验,如果存在且正确就将对应这个用户名的各项通讯录信息查询出来发送给客户端,这个时候服务端查询出来的是一个ResultSet的数据包,我通过while遍历可以把ResultSet里的字段一个个弄出来放到String里,然后通过N多个dos.writeUTF(str)一个个发送给客户端,这个时候有个问题,就是我不知道服务端要写多少dos.writeUTF(str)用来发送,客户端也不知要写多少个dis.readUTF()用来接收信息,写少了接收不全,写多了接收了别处的信息。如果我用while条件发送和接收,在服务端方可以通过while(ResultSet.next())一个一个发直到发到最后一个停止,但是客户端如果也用while()接收,那我怎么确定什么时候跳出循环呢?
展开
 我来答
风清月朗z0
2016-06-17 · TA获得超过545个赞
知道小有建树答主
回答量:400
采纳率:0%
帮助的人:162万
展开全部
1. Server代码:
package com.javacodegeeks.android.androidsocketserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class Server extends Activity
{
private ServerSocket serverSocket;
private Handler updateConversationHandler;
private Thread serverThread = null;
private TextView text;
public static final int SERVERPORT = 6000;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text = (TextView) findViewById(R.id.text2);

updateConversationHandler = new Handler();

this.serverThread = new Thread(new ServerThread());
this.serverThread.start();

}

@Override
protected void onStop()
{
super.onStop();
try
{
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

private class ServerThread implements Runnable
{

public void run()
{
Socket socket = null;
try
{
serverSocket = new ServerSocket(SERVERPORT);
}
catch (IOException e)
{
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted())
{
try
{
socket = serverSocket.accept(); // TODO

CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
}
catch (IOException e)
{
Log.i("liu", "socket.accept()失败");

e.printStackTrace();
}
}
}
}

private class CommunicationThread implements Runnable
{
private Socket clientSocket;
private BufferedReader input;

public CommunicationThread(Socket clientSocket)
{
this.clientSocket = clientSocket;

Log.i("liu", "获取到了client的Socket");

try
{
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); // TODO
}
catch (IOException e)
{
Log.i("liu", "input获取失败");
e.printStackTrace();
}
}

public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
String read = input.readLine(); // TODO
Log.i("liu", read);
updateConversationHandler.post(new updateUIThread(read));
}
catch (IOException e)
{
Log.i("liu", "input读取失败");
e.printStackTrace();
}
}
}
}

private class updateUIThread implements Runnable
{
private String msg;

public updateUIThread(String str)
{
this.msg = str;
}

@Override
public void run()
{
text.setText(text.getText().toString() + "Client Says: " + msg + "\n");
}

}

}
2. Client代码:

package com.javacodegeeks.android.androidsocketclient;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class Client extends Activity
{
private Socket socket;
private static final int SERVERPORT = 4000;
private static final String SERVER_IP = "10.0.2.2";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

new Thread(new ClientThread()).start();
}

public void onClick(View view)
{
try
{
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();

Log.i("liu", "点击按钮");

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // TODO
out.println(str);
}
catch (Exception e)
{
Log.i("liu", "write失败");
e.printStackTrace();
}
}

class ClientThread implements Runnable
{

@Override
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
3. 附件 是 客户端和服务端 的源码。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式