仿QQ简易聊天(安卓版)

2022-11-30 10:10:51 浏览数 (1)

客户端一:

package com.example.testqqclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements Runnable { private TextView textview1, textview2; private EditText edittext; private Button button; private PrintWriter writer; private BufferedReader reader; Socket socket; private Handler handler; private String write = ""; @Override protected void onStart() { // TODO 自动生成的方法存根 super.onStart(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) findViewById(R.id.textView1); textview2 = (TextView) findViewById(R.id.textView2); edittext = (EditText) findViewById(R.id.edittext); button = (Button) findViewById(R.id.button1); Thread t = new Thread(this); t.start(); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO 自动生成的方法存根 if (msg.what == 0x11) { textview2.setText("老人端发来的:" msg.getData().getString("reader") "n"); } super.handleMessage(msg); } }; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 write = edittext.getText().toString() "n"; textview1.setText("本人自己写的:" write); writer.print(edittext.getText().toString() "n"); writer.flush(); edittext.setText(""); } }); } @Override public void run() { try { socket = new Socket("localhost", 8100); writer = new PrintWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF-8"), true); reader = new BufferedReader(new InputStreamReader( socket.getInputStream(), "UTF-8")); Bundle bundle = new Bundle(); String str=""; while (!socket.isClosed()) { str = reader.readLine() "n"; bundle.putString("reader", str); Message m = handler.obtainMessage(); m.what = 0x11; m.setData(bundle); handler.sendMessage(m); } Thread.sleep(100); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override protected void onDestroy() { // TODO 自动生成的方法存根 if (reader != null) { System.out.println("reader.close();"); try { reader.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (socket != null) { System.out.println("socket.close()"); try { socket.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } super.onDestroy(); } }

客户端二:

package com.example.testqqclient2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements Runnable { private TextView textview1, textview2; private EditText edittext; private Button button; private PrintWriter writer; private BufferedReader reader; Socket socket; private Handler handler; private String write = ""; @Override protected void onStart() { // TODO 自动生成的方法存根 super.onStart(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) findViewById(R.id.textView1); textview2 = (TextView) findViewById(R.id.textView2); edittext = (EditText) findViewById(R.id.edittext); button = (Button) findViewById(R.id.button1); Thread t = new Thread(this); t.start(); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO 自动生成的方法存根 if (msg.what == 0x11) { textview2.setText("儿女发来的消息" msg.getData().getString("reader") "n"); } super.handleMessage(msg); } }; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 write = edittext.getText().toString() "n"; textview1.setText("本人自己写的:" write); writer.print(edittext.getText().toString() "n"); writer.flush(); edittext.setText(""); } }); } @Override public void run() { try { socket = new Socket("localhost", 8000); writer = new PrintWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF-8"), true); reader = new BufferedReader(new InputStreamReader( socket.getInputStream(), "UTF-8")); Bundle bundle = new Bundle(); String str=""; while (!socket.isClosed()) { str = reader.readLine() "n"; bundle.putString("reader", str); Message m = handler.obtainMessage(); m.what = 0x11; m.setData(bundle); handler.sendMessage(m); } Thread.sleep(100); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override protected void onDestroy() { // TODO 自动生成的方法存根 if (reader != null) { System.out.println("reader.close();"); try { reader.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (socket != null) { System.out.println("socket.close()"); try { socket.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } super.onDestroy(); } }

服务器:

package com.qg.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class QQServer implements Runnable { private BufferedReader reader; private PrintWriter writer; private ServerSocket server; private Socket socket ;     private QQServer qq=null;     private boolean flag1=true,flag2=true; public static void main(String[] args) { // TODO 自动生成的方法存根 //ArrayList<Socket> list=new ArrayList<Socket>(); QQServer qqserver = new QQServer(); QQServer qqserver2 = new QQServer(); qqserver.getServer(8000); qqserver2.getServer(8100); qqserver.qq=qqserver2; Thread t=new Thread(qqserver); t.start(); qqserver2.qq=qqserver; Thread t2=new Thread(qqserver2); t2.start(); } public boolean isFlag1() { return flag1; } public void setFlag1(boolean flag1) { this.flag1 = flag1; } public boolean isFlag2() { return flag2; } public void setFlag2(boolean flag2) { this.flag2 = flag2; } public Socket getServer(int id) { try { server = new ServerSocket(id);    socket = server.accept(); } catch (Exception e) { // TODO: handle exception } return socket; } @Override public void run() { // TODO 自动生成的方法存根 while(true){ try { if(!socket.isClosed()){ reader = new BufferedReader(new InputStreamReader( socket.getInputStream(), "UTF-8")); setFlag1(true); }else{ setFlag1(false); reader.close(); } if(!qq.socket.isClosed()){ writer = new PrintWriter(new OutputStreamWriter( qq.socket.getOutputStream(), "UTF-8"), true); setFlag2(true); }else{ setFlag2(false); writer.close(); } Thread.sleep(100); System.out.println("服务器连接成功。"); System.out.println("等待客户端的连接"); while (true) { String str=null; if(isFlag1()){ str = reader.readLine();//client1 System.out.println("str1=:" str "n"); } if(str==null){ System.out.println("break1....."); break; } Thread.sleep(100); if(isFlag2()){ writer.print(str "n"); writer.flush(); } if(writer==null){ System.out.println("break2....."); break; } } if (reader != null) { reader.close(); } if (socket!= null) { socket.close(); } if (qq.socket!= null) { socket.close(); } } catch (Exception e) { e.printStackTrace(); } } } }

0 人点赞