转载自::http://blog.csdn.net/u010142437/article/details/9327541
在Java中我们可以利用socket编程实现聊天室,在Android中也一样,因为Android完全支持JDK本身的TCP、UDP网络通信API。我们可以使用ServerSocket、Socket来建立基于TCP/IP协议的网络通信;也可以使用DatagramSocket、Datagrampacket、MulticastSocket来建立基于UDP协议的网络通信。下面实现一个简单的聊天室,服务器端完全用Java代码实现,跟Android无关,客户端用Android应用来实现。
服务器不断读取来自客户端的信息,并即时地将信息发送给每个连接到本服务器上的客户端,每个客户端可以向服务器发送消息,并不断地接收来自服务器的消息,并将消息显示在界面上。这样就实现了客户端与客户端之间的即时聊天功能。代码如下:
服务器端:
创建服务器的主类:
- package com.home.server;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- public class MyServer {
- // 定义保存所有Socket的集合
- public static ArrayList<Socket> socketList = new ArrayList<Socket>();
- public static void main(String[] args) throws IOException {
- ServerSocket ss = new ServerSocket(20000);
- System.out.println("服务器创建成功!");
- System.out.println("等待客戶端的连接。。。");
- while (true) {
- // 此行代码会阻塞,等待用户的连接
- Socket socket = ss.accept();
- System.out.println("有客户端连接进来!");
- socketList.add(socket);
- // 每当客户端连接后启动一条ServerThread线程为该客户端服务
- new Thread(new ServerThread(socket)).start();
- }
- }
- }
服务器的线程类:
- package com.home.server;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.Socket;
- public class ServerThread implements Runnable {
- // 定义当前线程所处理的Socket
- private Socket socket = null;
- // 该线程所处理的Socket所对应的输入流
- BufferedReader br = null;
- public ServerThread(Socket socket) throws IOException {
- this.socket = socket;
- // 初始化该Socket对应的输入流
- br = new BufferedReader(new InputStreamReader(socket.getInputStream(),
- "utf-8"));
- }
- @Override
- public void run() {
- try {
- String content = null;
- // 采用循环不断从Socket中读取客户端发送过来的数据
- while ((content = readFromClient()) != null) {
- // 遍历socketList中的每个Socket,将读到的内容向每个Socket发送一次
- for (Socket s : MyServer.socketList) {
- OutputStream os = s.getOutputStream();
- os.write((content + "\n").getBytes("utf-8"));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 定义读取客户端数据的方法
- *
- * @return
- */
- private String readFromClient() {
- try {
- return br.readLine();
- }
- // 如果捕捉到异常,表明该Socket对应的客户端已经关闭
- catch (Exception e) {
- // 删除该Socket
- MyServer.socketList.remove(socket);
- e.printStackTrace();
- }
- return null;
- }
- }
客户端主类(Activity):
- package com.home.activity;
- import java.io.OutputStream;
- import java.net.Socket;
- import android.app.Activity;
- 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 com.home.R;
- import com.home.util.ClientThread;
- public class MultiThreadClient extends Activity {
- private EditText input, show;
- private Button sendBtn;
- private OutputStream os;
- private Handler handler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- input = (EditText) findViewById(R.id.main_et_input);
- show = (EditText) findViewById(R.id.main_et_show);
- sendBtn = (Button) findViewById(R.id.main_btn_send);
- handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // 如果消息来自子线程
- if (msg.what == 0x234) {
- // 将读取的内容追加显示在文本框中
- show.append("\n" + msg.obj.toString());
- }
- }
- };
- Socket socket;
- try {
- socket = new Socket("192.168.0.101", 20000);
- // 客户端启动ClientThread线程不断读取来自服务器的数据
- new Thread(new ClientThread(socket, handler)).start();
- os = socket.getOutputStream();
- } catch (Exception e) {
- e.printStackTrace();
- }
- sendBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- // 将用户在文本框内输入的内容写入网络
- os.write((input.getText().toString() + "\r\n").getBytes());
- // 清空input文本框数据
- input.setText("");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
客户端线程类:
- package com.home.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import android.os.Handler;
- import android.os.Message;
- public class ClientThread implements Runnable {
- private Handler handler;
- // 该线程所处理的Socket所对应的输入流
- private BufferedReader br = null;
- public ClientThread(Socket socket, Handler handler) throws IOException {
- this.handler = handler;
- br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- }
- @Override
- public void run() {
- try {
- String content = null;
- // 不断读取Socket输入流的内容
- while ((content = br.readLine()) != null) {
- // 每当读到来自服务器的数据之后,发送消息通知程序界面显示该数据
- Message msg = new Message();
- msg.what = 0x234;
- msg.obj = content;
- handler.sendMessage(msg);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Activity的布局XML:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/main_et_input"
- android:layout_width="240dp"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/main_btn_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="10dp"
- android:text="发送" />
- </LinearLayout>
- <EditText
- android:id="@+id/main_et_show"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:cursorVisible="false"
- android:editable="false"
- android:gravity="top" />
- </LinearLayout>
权限:
- <uses-permission android:name="android.permission.INTERNET"/>