Android实现C/S聊天室
Java中能接受其他通信实体链接请求的类是ServerSocket,ServerSocket对象用于监听来自客户端的Socket链接,如果没有链接,它将一直等待。如果接收到一个客户端Socket的连接请求,ServerSocket的accept()方法将返回一个与客户端Socket对应的Socket(每个TCP连接有两个Socket),否则该方法将一直阻塞,线程也被阻塞。
服务端思路:服务端应该包含多个线程,每个Socket对应一个线程,这个线程负责读取该Socket对应输入流的数据(从客户端发送过来的数据),并将读到的数据向每个Socket输出流发送一次(将一个客户端发送过来的数据“广播”给其他客户端)。
服务端代码:
//服务端主类 public class MyServer { public static List<Socket> socketList = Collections.synchronizedList(new ArrayList<Socket>()); public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(30000); while (true) { //此行代码会阻塞,将一直等待别人的连接 Socket s = ss.accept(); socketList.add(s); //每当客户端连接后启动一个ServerThread线程为该客户端服务 new Thread(new ServerThread(s)).start(); } } }
public class ServerThread implements Runnable { //定义当前线程所处理的Socket Socket s = null; //该线程所处理的Socket对应的输入流 BufferedReader br = null; public ServerThread(Socket s) throws IOException { this.s = s; //初始化该Socket对应的输入流 br = new BufferedReader(new InputStreamReader(s.getInputStream())); } @Override public void run() { try { String content = null; //采用循环不断地从Socket中读取客户端发送来的数据 while ((content = readFromClient()) != null) { //遍历socketList中的每个Socket //将读到的内容向每个Socket发送一次 for (Socket s : MyServer.socketList) { PrintStream ps = new PrintStream(s.getOutputStream()); ps.println(content); } } } catch (IOException e) { e.printStackTrace(); } } //定义读取客户端数据的方法 private String readFromClient() { try { return br.readLine(); } //如果捕获到异常,则表明该Socket对应的客户端已经关闭 catch (IOException e) { //删除该Socket MyServer.socketList.remove(s); } return null; } }
客户端思路:将用户输入的数据写入Socket对应的输入流中;开启一个子线程读取Socket对应输入流中的数据(从服务端发送过来的数据),并通过Handler将读取的数据发送到主线程来更新UI。
//用户界面Activity public class MainActivity extends Activity { private EditText mReceiverMsg; private Button mSendBtn; private EditText mSendMsg; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Log.d("mainActivity" , "okk"); mReceiverMsg.append(msg.obj.toString()); } }; private Socket s; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); initView(); initSocket(); mSendBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sendData(); } }); } private void initSocket() { new Thread() { @Override public void run() { try { s = new Socket("192.168.1.101" , 30000); new Thread(new ClientThread(s , handler)).start(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } private void initView() { mReceiverMsg = (EditText) findViewById(R.id.receiver_message); mSendMsg = (EditText) findViewById(R.id.send_message); mSendBtn = (Button) findViewById(R.id.send_button); } private void sendData() { try { //获取该Socket对应的输出流 PrintStream ps = new PrintStream(s.getOutputStream()); if (TextUtils.isEmpty(mSendMsg.getText())) { Toast.makeText(this , "请输入信息" , Toast.LENGTH_LONG).show(); return; } ps.println(mSendMsg.getText().toString()); } catch (IOException e) { e.printStackTrace(); } } }
public class ClientThread implements Runnable { //该线程负责处理的Socket private Socket ss; //该线程所处理的Socket对应的输入流 BufferedReader br = null; Handler handler; public ClientThread(Socket s , Handler handler) throws IOException { this.ss = s; this.handler = handler; br = new BufferedReader(new InputStreamReader(ss.getInputStream())); } @Override public void run() { try { String content = null; while ((content = br.readLine()) != null) { Message msg = new Message(); msg.obj = content; handler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); } } }
先运行上面程序中的MyServer类,该类运行只是作为服务端。再启动多个模拟器,运行安装客户端的程序作为多个客户端,然后可以再任何一个客户端通过Edit输入一些内容,点击发送就可以在任何一个客户端看到刚刚输入的内容。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)