安卓socket聊天

安卓基於Socket通訊(服務器配合)java

1.話很少說進入正題,先建立服務端,在Android Studio中建立Java代碼,以下圖所示:android

選擇Java Library 須要更名字的本身隨意git

 

2.建立Client Manager客戶端管理類來管理客戶端的消息,由於省時間就直接從我上篇博客的代碼基礎上進行的修改~代碼以下所示:(本身編寫代碼塊提交後總有亂碼...因此只能把本身的代碼複製粘貼進來啦~格式有點奇怪,可是沒有亂碼~)github

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sp01 on 2017/4/28.
 */
// 客戶端的管理類
public class ClientManager {

    private static Map<String,Socket> clientList = new HashMap<>();
    private static ServerThread serverThread = null;

    private static class ServerThread implements Runnable {

        private int port = 10010;
        private boolean isExit = false;
        private ServerSocket server;

        public ServerThread() {
            try {
                server = new ServerSocket(port);
                System.out.println("啓動服務成功" + "port:" + port);
            } catch (IOException e) {
                System.out.println("啓動server失敗,錯誤緣由:" + e.getMessage());
            }
        }

        @Override
        public void run() {
            try {
                while (!isExit) {
                    // 進入等待環節
                    System.out.println("等待手機的鏈接... ... ");
                    final Socket socket = server.accept();
                    // 獲取手機鏈接的地址及端口號
                    final String address = socket.getRemoteSocketAddress().toString();
                    System.out.println("鏈接成功,鏈接的手機爲:" + address);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                // 單線程索鎖
                                synchronized (this){
                                    // 放進到Map中保存
                                    clientList.put(address,socket);
                                }
                                // 定義輸入流
                                InputStream inputStream = socket.getInputStream();
                                byte[] buffer = new byte[1024];
                                int len;
                                while ((len = inputStream.read(buffer)) != -1){
                                    String text = new String(buffer,0,len);
                                    System.out.println("收到的數據爲:" + text);
                                    // 在這裏羣發消息
                                    sendMsgAll(text);
                                }

                            }catch (Exception e){
                                System.out.println("錯誤信息爲:" + e.getMessage());
                            }finally {
                                synchronized (this){
                                    System.out.println("關閉連接:" + address);
                                    clientList.remove(address);
                                }
                            }
                        }
                    }).start();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void Stop(){
            isExit = true;
            if (server != null){
                try {
                    server.close();
                    System.out.println("已關閉server");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static ServerThread startServer(){
        System.out.println("開啓服務");
        if (serverThread != null){
            showDown();
        }
        serverThread = new ServerThread();
        new Thread(serverThread).start();
        System.out.println("開啓服務成功");
        return serverThread;
    }

    // 關閉全部server socket 和 清空Map
    public static void showDown(){
        for (Socket socket : clientList.values()) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        serverThread.Stop();
        clientList.clear();
    }

    // 羣發的方法
    public static boolean sendMsgAll(String msg){
        try {
            for (Socket socket : clientList.values()) {
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(msg.getBytes("utf-8"));
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

}

 

 

 

代碼看起來比較簡單,用了儘量方便理解的書寫,也寫好了一些註釋,應該不難理解因此就不具體解釋了,對Server Socket有不理解的地方,請參考個人上篇博客~但願能有所幫助,但須要解釋的地方可能只有一點吧,羣發的方法對收到的消息所有進行廣播式的發送,那麼不就發送的人也會收到消息了嘛?(可能有人感受會有數據顯示重複的狀況)我想說的是,真正歷史記錄都會在服務端進行數據保存和處理這樣想就好了,我在Android端作了一個RecyclerView的加載不一樣行佈局實現模擬聊天界面,發送和接收的歷史消息都會顯示在列表上,本人發送的內容在左側,其餘人發送的消息被顯示在右側。數組

 

3.在MaClass.java(主入口類)中開啓服務:服務器

 

public class MyClass {

    public static void main(String[]args){
        // 開啓服務器
        ClientManager.startServer();
    }

}
 

 

 

4.到這裏爲止,服務端的代碼就完成了很簡單,有人運行代碼時,會出現控制檯中文亂碼狀況,解決辦法我轉發的博客中有介紹,可是考慮到有人很懶,不想在麻煩去找,我就直接在下面介紹了,很簡單隻須要一句話:app

 

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}
 

 

 

 

複製代碼塊,放進藍色的gradle位置中(Java lib包內)dependencies{}下方位置,在Rebuild一下就行了。socket

 

5.新建並編寫Android客戶端工程,大體內容就是一個EditText輸入框,點擊按鈕發送數據,上方爲一個加載不一樣行佈局的RecyclerView,實現歷史記錄閱覽,下面是activity_main.xml的內容:ide

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sq.test_socketchat.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <EditText
            android:id="@+id/et"
            android:layout_weight="8"
            android:layout_width="0dp"
            android:hint="輸入內容"
            android:layout_height="match_parent" />

        <Button
            android:id="@+id/btn"
            android:text="發送"
            android:layout_margin="3dp"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>

 

 

 

顯示效果如上圖所示。佈局

 

6.接下來是準備工做,首先寫一個MyBean,用來存儲名字,消息內容,消息時間,以及加載哪一種佈局:

 

/**
 * Created by sp01 on 2017/4/28.
 */
public class MyBean {
    private String data;
    private String time,name;
    private int number;

    public MyBean() {
    }

    public MyBean(String data, int number,String time,String name) {
        this.data = data;
        this.number = number;
        this.name = name;
        this.time = time;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

 

 

 

7.一樣是準備工做,兩個不一樣佈局的item的書寫,第一種內容顯示在左側第二種則在右側,直接複製個人就好:

第一個item:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#c8fffa"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv"
        android:layout_gravity="left"
        android:textSize="20sp"
        android:text="lalala"
        android:layout_margin="5dp"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_gravity="left"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_name"
            android:text="name_xx"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_margin="5dp"
            android:text="1993-09-28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
    
</LinearLayout>
 

 

第二個item:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#fcfdd9"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv2"
        android:layout_gravity="right"
        android:textSize="20sp"
        android:text="lalala"
        android:layout_margin="5dp"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_gravity="right"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_name2"
            android:text="name_xx"
            android:layout_margin="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_time2"
            android:layout_margin="5dp"
            android:text="1993-09-28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>
 

 

 

8.接下來是書寫MyAdapter內的代碼(RecyclerView加載不一樣行佈局很簡單就不過多強調了):

 

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

/**
 * Created by sp01 on 2017/4/28.
 */
public class MyAdapter extends RecyclerView.Adapter {

    private Context context;
    private ArrayList<MyBean> data;
    private static final int TYPEONE = 1;
    private static final int TYPETWO = 2;

    public MyAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<MyBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getNumber();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case TYPEONE:
                View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
                holder = new OneViewHolder(view);
                break;
            case TYPETWO:
                View view1 = LayoutInflater.from(context).inflate(R.layout.item2,parent,false);
                holder = new TwoViewHolder(view1);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case TYPEONE:
                OneViewHolder oneViewHolder = (OneViewHolder) holder;
                oneViewHolder.tv1.setText(data.get(position).getData());
                oneViewHolder.name1.setText(data.get(position).getName());
                oneViewHolder.time1.setText(data.get(position).getTime());
                break;
            case TYPETWO:
                TwoViewHolder twoViewHolder = (TwoViewHolder) holder;
                twoViewHolder.tv2.setText(data.get(position).getData());
                twoViewHolder.name2.setText(data.get(position).getName());
                twoViewHolder.time2.setText(data.get(position).getTime());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return  data != null && data.size() > 0 ? data.size() : 0;
    }
    
    class OneViewHolder extends RecyclerView.ViewHolder{
       private TextView tv1;
        private TextView name1,time1;
       public OneViewHolder(View itemView) {
           super(itemView);
           tv1 = (TextView) itemView.findViewById(R.id.tv);
           name1 = (TextView) itemView.findViewById(R.id.tv_name);
           time1 = (TextView) itemView.findViewById(R.id.tv_time);
       }
   }

    class TwoViewHolder extends RecyclerView.ViewHolder{
        private TextView tv2;
        private TextView name2,time2;
        public TwoViewHolder(View itemView) {
            super(itemView);
            tv2 = (TextView) itemView.findViewById(R.id.tv2);
            name2 = (TextView) itemView.findViewById(R.id.tv_name2);
            time2 = (TextView) itemView.findViewById(R.id.tv_time2);
        }
    }
    
}
 

 

 

9.下面終於進入到了正題~進入到MainActivity中,代碼以下所示:

 

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    private EditText et;
    private Button btn;
    private Socket socket;
    private ArrayList<MyBean> list;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = (RecyclerView) findViewById(R.id.rv);
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        list = new ArrayList<>();
        adapter = new MyAdapter(this);

        final Handler handler = new MyHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket("192.168.1.111", 10010);
                    InputStream inputStream = socket.getInputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buffer)) != -1) {
                        String data = new String(buffer, 0, len);
                        // 發到主線程中 收到的數據
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = data;
                        handler.sendMessage(message);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String data = et.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            OutputStream outputStream = socket.getOutputStream();
                            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");    //設置日期格式
                            outputStream.write((socket.getLocalPort() + "//" + data + "//" + df.format(new Date())).getBytes("utf-8"));
                            outputStream.flush();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

    }

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                //
                int localPort = socket.getLocalPort();
                String[] split = ((String) msg.obj).split("//");
                if (split[0].equals(localPort + "")) {
                    MyBean bean = new MyBean(split[1],1,split[2],"我:");
                    list.add(bean);
                } else {
                    MyBean bean = new MyBean(split[1],2,split[2],("來自:" + split[0]));
                    list.add(bean);
                }

                // 向適配器set數據
                adapter.setData(list);
                rv.setAdapter(adapter);
                LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
                rv.setLayoutManager(manager);
            }
        }
    }
}

 

 

 

代碼很簡單,由於Socket發送的數據只能是一個基本的數據類型,不能傳遞相似於HashMap、集合、數組這樣的數據,因此只能經過拼接字符串的形式經過加入一些特殊的符號,來起到分割數據的做用,由於傳遞的數據中帶有發送者、接受者、時間、消息等這樣的數據,因此經過split來區別這些數據,從而進行具體的分配來實現目的。

 

10.最後權限不要忘記加入~

 

<uses-permission android:name="android.permission.INTERNET"/>

 

 

 

那麼運行實現的具體效果又是怎樣的呢?請看下面(話說CSDN加圖好麻煩啊):

1)這是開啓服務器以後,兩臺手機打開聊天Demo:

 

2)這是客戶端發送數據的顯示內容:

 

3)這是服務端在客戶端聊天時顯示的Log:

 

Demo:https://github.com/shiqiangdva/Socket_Chat

 

--------------------轉載至https://blog.csdn.net/qq_37842258/article/details/70945192

相關文章
相關標籤/搜索