1。首先在安卓上创建客户端,并加入线程;
2.建一个java工程,创建服务端,写入线程;
创建客户端:
privateButtonbtnMain;
privateTextViewtvMain;
privateClientThreadmClientThread;
//在主线程中定义Handler传入子线程用于更新TextView
privateHandlerhandler;
@Override
protected voidonCreate(Bundle savedInstanceState) {//定义控件
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
etMain= (EditText) findViewById(R.id.et_main);
btnMain= (Button) findViewById(R.id.btn_main);
tvMain= (TextView) findViewById(R.id.tv_main);
mHander=newHander(){//将子线程的的信息在主线程中显示出来
Override
public voidhandleMessage(Message msg) {
if(msg.what==0) {//得到服务器返回的信息
tvMain.append("\n"+ msg.obj.toString());
}
}
};
//点击button时,获取EditText中string并且调用子线程的Handler发送到服务器
Handler mHandler;
btnMain.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
try{//得到输入框中的内容,写到输入流中
Message msg =newMessage();
msg.what=1;
msg.obj=etMain.getText().toString();
mClientThread.revHandler.sendMessage(msg);
etMain.setText("");
}catch(Exception e) {
e.printStackTrace();
}
}
});
mClientThread=newClientThread(mHandler);
newThread(mClientThread).start();
}
}
(此段是将文本框中的信息截取出来,调用子线程,发送到服务器中)
ClientThread:
ublicClientThread(Handler handler) {
mHandler= handler;
}
@Override
public voidrun() {
try{
mSocket=newSocket(" 192.168.56.1",1);//将客户端与服务端相连????????????????????????????????????
mBufferedReader=newBufferedReader(newInputStreamReader(mSocket.getInputStream()));
mOutputStream=mSocket.getOutputStream();//将其封装成bufferedreader
newThread() {//开一个线程
@Override
public voidrun() {
super.run();
try{//一直与服务端通信
String content =null;
while((content =mBufferedReader.readLine()) !=null) {
Log.d("xjj", content);
Message msg =newMessage();
msg.what=0;
msg.obj= content;
mHandler.sendMessage(msg);
}
}catch(IOException e) {
e.printStackTrace();
}
}
}.start();
Looper.prepare();//显示信息。。。。。。
revHandler=newHandler() {
@Override
public voidhandleMessage(Message msg) {
if(msg.what==1) {//得到服务器返回的信息
try{
mOutputStream.write((msg.obj.toString() +"\r\n").getBytes("utf-8"));
}catch(IOException e) {
e.printStackTrace();
}
服务端:public classsever {
public staticArrayListsocketList=newArrayList();
public staticStringcontent="";
public static voidmain(String[] args)throwsIOException {
//建立ServerSocket
ServerSocket ss =newServerSocket(1);//指明服务端口号
while(true) {//服务端与客户端相连
Socket s = ss.accept();//j接受一个客户
System.out.println("connect success!");
socketList.add(s);//接受并储存到socketlist中
newThread(newServerThread(s)).start();
}
}
创建线程ServerThread:
ublic classServerThreadimplementsRunnable {
privateSocketmSocket=null;
privateBufferedReadermBufferedReader=null;
// 接收socket并且初始化BufferedReader
publicServerThread(Socket socket)
throwsUnsupportedEncodingException, IOException {
//??????????????????????????????????????????????????????????????????????/
mSocket= socket;
mBufferedReader=newBufferedReader(
newInputStreamReader(mSocket.getInputStream(),"utf-8"));
}
@Override
public voidrun() {
try{
String content =null;
//接受来自客户端的信息
while((content =mBufferedReader.readLine()) !=null) {
System.out.println(content);//向客户端发送数据
//如果异常,说明已断开,将该条目从socklist中移除
for(Iterator it = sever.socketList.iterator();
it.hasNext();) {
Socket s = it.next();
for(it = sever.socketList.iterator();
it.hasNext();) {
s = it.next();
it.remove();
}
}
}
}catch(IOException e) {
e.printStackTrace();
sever.socketList.remove(mSocket);
}