├── .gitattributes
├── .gitignore
├── README.md
├── android
└── XG-ChatRoom
│ ├── .gitignore
│ ├── XG-ChatRoom.iml
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── xg
│ │ └── chat
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── xg
│ │ │ └── chat
│ │ │ ├── bean
│ │ │ └── ChatBean.java
│ │ │ ├── socket
│ │ │ └── SocketThread.java
│ │ │ ├── utils
│ │ │ └── ToastUtils.java
│ │ │ └── view
│ │ │ ├── ChatAdapter.java
│ │ │ ├── ChatRoomActivity.java
│ │ │ ├── ChatView.java
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable-xhdpi
│ │ ├── rc_bg_text_hover.9.png
│ │ ├── rc_ic_bubble_no_left.9.png
│ │ └── rc_ic_bubble_no_right.9.png
│ │ ├── layout
│ │ ├── activity_chat_room.xml
│ │ ├── activity_main.xml
│ │ └── lv_item_chat.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── xg
│ └── chat
│ └── ExampleUnitTest.java
├── images
├── 20160416141020.png
├── 20160416141118.png
├── 20160416141148.png
├── 20160416141205.png
└── 20160416161021.png
└── java
└── Server
├── .classpath
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── libs
└── gson-2.3.1.jar
└── src
└── com
└── xg
└── server
├── Main.java
├── data
├── DiscussionGroups.java
└── model
│ └── ChatBean.java
└── socket
└── Service.java
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | */build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # =========================
36 | # Operating System Files
37 | # =========================
38 |
39 | # OSX
40 | # =========================
41 |
42 | .DS_Store
43 | .AppleDouble
44 | .LSOverride
45 |
46 | # Thumbnails
47 | ._*
48 |
49 | # Files that might appear in the root of a volume
50 | .DocumentRevisions-V100
51 | .fseventsd
52 | .Spotlight-V100
53 | .TemporaryItems
54 | .Trashes
55 | .VolumeIcon.icns
56 |
57 | # Directories potentially created on remote AFP share
58 | .AppleDB
59 | .AppleDesktop
60 | Network Trash Folder
61 | Temporary Items
62 | .apdisk
63 |
64 | # Windows
65 | # =========================
66 |
67 | # Windows image file caches
68 | Thumbs.db
69 | ehthumbs.db
70 |
71 | # Folder config file
72 | Desktop.ini
73 |
74 | # Recycle Bin used on file shares
75 | $RECYCLE.BIN/
76 |
77 | # Windows Installer files
78 | *.cab
79 | *.msi
80 | *.msm
81 | *.msp
82 |
83 | # Windows shortcuts
84 | *.lnk
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android-Socket-ChatRoom
2 | ## Android Socket 实现群聊功能
3 | 工程分为两个端,java和Android,效果图如下:
4 | 
5 | 
6 | 
7 | 
8 | 
9 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/XG-ChatRoom.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "24.0.0 rc2"
6 |
7 | defaultConfig {
8 | applicationId "com.xg.chat"
9 | minSdkVersion 15
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.3.0'
26 | compile 'com.jakewharton:butterknife:7.0.1'
27 | compile 'com.google.code.gson:gson:2.6.2'
28 | }
29 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\sdk\android-sdk-windows/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/androidTest/java/com/xg/chat/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/bean/ChatBean.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.bean;
2 |
3 | /**
4 | * Created by Administrator on 2016/4/16 0016.
5 | */
6 | public class ChatBean {
7 | public String content;
8 | public String name;
9 | public long time;
10 |
11 | public ChatBean(String content, String name) {
12 | this.content = content;
13 | this.name = name;
14 | time = System.currentTimeMillis();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/socket/SocketThread.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.socket;
2 |
3 | import android.os.Handler;
4 | import android.os.Message;
5 | import android.text.TextUtils;
6 |
7 | import com.google.gson.Gson;
8 | import com.xg.chat.view.ChatView;
9 | import com.xg.chat.bean.ChatBean;
10 |
11 | import java.io.BufferedReader;
12 | import java.io.BufferedWriter;
13 | import java.io.IOException;
14 | import java.io.InputStreamReader;
15 | import java.io.OutputStreamWriter;
16 | import java.io.PrintWriter;
17 | import java.net.Socket;
18 | import java.net.UnknownHostException;
19 |
20 | /**
21 | * Created by Administrator on 2016/4/16 0016.
22 | */
23 | public class SocketThread extends Thread {
24 |
25 | private Socket socket = null;
26 | private BufferedReader in = null;
27 | private PrintWriter out = null;
28 |
29 | private ChatView chatView;
30 |
31 | public SocketThread(ChatView chatView) {
32 | this.chatView = chatView;
33 | }
34 |
35 | private void init() {
36 | try {
37 | socket = new Socket(chatView.getHost(), Integer.parseInt(chatView.getProt()));
38 | in = new BufferedReader(new InputStreamReader(socket
39 | .getInputStream()));
40 | out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
41 | socket.getOutputStream())), true);
42 | if (!socket.isOutputShutdown()) {
43 | ChatBean bean = new ChatBean("join", chatView.getUserId());
44 | out.println(new Gson().toJson(bean));
45 | }
46 | } catch (UnknownHostException e1) {
47 | e1.printStackTrace();
48 | mHandler.sendEmptyMessage(-1);
49 | } catch (IOException ex) {
50 | ex.printStackTrace();
51 | mHandler.sendEmptyMessage(0);
52 | } catch (IllegalArgumentException e2) {
53 | e2.printStackTrace();
54 | mHandler.sendEmptyMessage(-2);
55 | }
56 | }
57 |
58 | public void sendMsg(String msg) {
59 | ChatBean bean = new ChatBean(msg, chatView.getUserId());
60 | if (!TextUtils.isEmpty(msg) && socket != null && socket.isConnected()) {
61 | if (!socket.isOutputShutdown()) {
62 | out.println(new Gson().toJson(bean));
63 | }
64 | }
65 | }
66 |
67 | //接收线程发送过来信息,并用TextView显示
68 | public Handler mHandler = new Handler() {
69 | public void handleMessage(Message msg) {
70 | super.handleMessage(msg);
71 | switch (msg.what) {
72 | case -2:
73 | chatView.showDiaolg("IllegalArgumentException");
74 | break;
75 | case -1:
76 | chatView.showDiaolg("UnknownHostException");
77 | break;
78 | case 0:
79 | chatView.showDiaolg("IOException");
80 | break;
81 | case 1:
82 | String content = (String) msg.obj;
83 | ChatBean bean = new Gson().fromJson(content, ChatBean.class);
84 | chatView.receiveMsg(bean);
85 | break;
86 | }
87 | }
88 | };
89 |
90 | @Override
91 | public void run() {
92 | super.run();
93 | init();
94 |
95 | try {
96 | while (true) {
97 | if (!socket.isClosed()) {
98 | if (socket.isConnected()) {
99 | if (!socket.isInputShutdown()) {
100 | String content;
101 | if ((content = in.readLine()) != null) {
102 | content += "\n";
103 | Message message = mHandler.obtainMessage();
104 | message.obj = content;
105 | message.what = 1;
106 | mHandler.sendMessage(message);
107 | } else {
108 |
109 | }
110 | }
111 | }
112 | }
113 | }
114 | } catch (Exception e) {
115 | e.printStackTrace();
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/utils/ToastUtils.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.utils;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.StringRes;
5 | import android.widget.Toast;
6 |
7 | /**
8 | * Created by Administrator on 2016/4/15 0015.
9 | */
10 | public class ToastUtils {
11 | public static void makeToast(Context context, String msg) {
12 | Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
13 | }
14 |
15 | public static void makeToast(Context context,@StringRes int msgId) {
16 | Toast.makeText(context, context.getText(msgId), Toast.LENGTH_SHORT).show();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/view/ChatAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.view;
2 |
3 | import android.content.Context;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.BaseAdapter;
8 | import android.widget.FrameLayout;
9 | import android.widget.TextView;
10 |
11 | import com.xg.chat.R;
12 | import com.xg.chat.bean.ChatBean;
13 |
14 | import java.text.SimpleDateFormat;
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 | import butterknife.Bind;
19 | import butterknife.ButterKnife;
20 |
21 | /**
22 | * Created by Administrator on 2016/4/16 0016.
23 | */
24 | public class ChatAdapter extends BaseAdapter {
25 |
26 | private List mList;
27 | private LayoutInflater mInflater;
28 | private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
29 | private String userId;
30 |
31 | public ChatAdapter(Context ctx, String userId) {
32 | mInflater = LayoutInflater.from(ctx);
33 | mList = new ArrayList<>();
34 | this.userId = userId;
35 | }
36 |
37 | public void addChatBean(ChatBean chatBean) {
38 | if (chatBean == null) return;
39 | mList.add(chatBean);
40 | notifyDataSetChanged();
41 | }
42 |
43 | @Override
44 | public int getCount() {
45 | return mList.size();
46 | }
47 |
48 | @Override
49 | public Object getItem(int position) {
50 | return mList.get(position);
51 | }
52 |
53 | @Override
54 | public long getItemId(int position) {
55 | return position;
56 | }
57 |
58 | @Override
59 | public View getView(int position, View convertView, ViewGroup parent) {
60 | if (convertView == null) {
61 | convertView = mInflater.inflate(R.layout.lv_item_chat, parent, false);
62 | convertView.setTag(new ViewHolder(convertView));
63 | }
64 | ViewHolder holder = (ViewHolder) convertView.getTag();
65 | holder.init(position);
66 | return convertView;
67 | }
68 |
69 | class ViewHolder {
70 | @Bind(R.id.tvTime)
71 | TextView tvTime;
72 | @Bind(R.id.tvLeft)
73 | TextView tvLeft;
74 | @Bind(R.id.tvRight)
75 | TextView tvRight;
76 | @Bind(R.id.tvTip)
77 | TextView tvTip;
78 | @Bind(R.id.frameLeft)
79 | FrameLayout frameLeft;
80 | @Bind(R.id.frameRight)
81 | FrameLayout frameRight;
82 |
83 | public ViewHolder(View v) {
84 | ButterKnife.bind(this, v);
85 | }
86 |
87 | public void init(int position) {
88 | ChatBean bean = mList.get(position);
89 | if (position == 0 || position > 0 && bean.time - mList.get(position - 1).time > 60000) {
90 | tvTime.setVisibility(View.VISIBLE);
91 | tvTime.setText(format.format(bean.time));
92 | } else {
93 | tvTime.setVisibility(View.GONE);
94 | }
95 | frameLeft.setVisibility(View.GONE);
96 | frameRight.setVisibility(View.GONE);
97 | if (bean.content.equals("exit")) {
98 | tvTip.setVisibility(View.VISIBLE);
99 | tvTip.setText(bean.name.equals(userId) ? "您已退出讨论组" : bean.name + "已退出讨论组");
100 | } else if (bean.content.equals("join")) {
101 | tvTip.setVisibility(View.VISIBLE);
102 | tvTip.setText(bean.name.equals(userId) ? "您已加入讨论组" : bean.name + "加入讨论组");
103 | } else {
104 | tvTip.setVisibility(View.GONE);
105 | frameLeft.setVisibility(bean.name.equals(userId) ? View.GONE : View.VISIBLE);
106 | frameRight.setVisibility(bean.name.equals(userId) ? View.VISIBLE : View.GONE);
107 | if (bean.name.equals(userId)) {
108 | tvRight.setText(bean.content);
109 | } else {
110 | tvLeft.setText(bean.content);
111 | }
112 | }
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/view/ChatRoomActivity.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.view;
2 |
3 | import android.content.DialogInterface;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AlertDialog;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.EditText;
10 | import android.widget.ListView;
11 |
12 | import com.xg.chat.R;
13 | import com.xg.chat.bean.ChatBean;
14 | import com.xg.chat.socket.SocketThread;
15 |
16 | import butterknife.Bind;
17 | import butterknife.ButterKnife;
18 | import butterknife.OnClick;
19 |
20 | public class ChatRoomActivity extends AppCompatActivity implements ChatView {
21 | @Bind(R.id.listView)
22 | ListView listView;
23 | @Bind(R.id.etContent)
24 | EditText etContent;
25 | private ChatAdapter mAdapter;
26 |
27 | private String host;
28 | private String prot;
29 |
30 |
31 | private String userId = String.valueOf(System.currentTimeMillis());
32 |
33 | private SocketThread thread;
34 |
35 | @Override
36 | protected void onCreate(Bundle savedInstanceState) {
37 | super.onCreate(savedInstanceState);
38 | setContentView(R.layout.activity_chat_room);
39 | ButterKnife.bind(this);
40 |
41 | Intent intent = getIntent();
42 | host = intent.getStringExtra("host");
43 | prot = intent.getStringExtra("prot");
44 |
45 | mAdapter = new ChatAdapter(this, userId);
46 | listView.setAdapter(mAdapter);
47 |
48 |
49 | //启动线程,接收服务器发送过来的数据
50 | thread = new SocketThread(this);
51 | thread.start();
52 | }
53 |
54 | /**
55 | * 如果连接出现异常,弹出AlertDialog!
56 | */
57 | public void ShowDialog(String msg) {
58 | new AlertDialog.Builder(this).setTitle("notification").setMessage(msg)
59 | .setPositiveButton("ok", new DialogInterface.OnClickListener() {
60 |
61 | @Override
62 | public void onClick(DialogInterface dialog, int which) {
63 |
64 | }
65 | }).show();
66 | }
67 |
68 | @OnClick(R.id.btnSend)
69 | public void onClick(View view) {
70 | String msg = etContent.getText().toString();
71 | etContent.setText(null);
72 | thread.sendMsg(msg);
73 | }
74 |
75 | @Override
76 | protected void onDestroy() {
77 | super.onDestroy();
78 | thread.sendMsg("exit");
79 | thread.interrupt();
80 | }
81 |
82 | @Override
83 | public String getHost() {
84 | return host;
85 | }
86 |
87 | @Override
88 | public String getProt() {
89 | return prot;
90 | }
91 |
92 | @Override
93 | public String getUserId() {
94 | return userId;
95 | }
96 |
97 | @Override
98 | public void showDiaolg(String msg) {
99 | new AlertDialog.Builder(this).setMessage(msg).setOnCancelListener(new DialogInterface.OnCancelListener() {
100 | @Override
101 | public void onCancel(DialogInterface dialog) {
102 | finish();
103 | }
104 | }).show();
105 | }
106 |
107 | @Override
108 | public void receiveMsg(ChatBean bean) {
109 | mAdapter.addChatBean(bean);
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/view/ChatView.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.view;
2 |
3 | import com.xg.chat.bean.ChatBean;
4 |
5 | /**
6 | * Created by Administrator on 2016/4/16 0016.
7 | */
8 | public interface ChatView {
9 |
10 | public String getHost();
11 | public String getProt();
12 | public String getUserId();
13 | public void showDiaolg(String msg);
14 | public void receiveMsg(ChatBean bean);
15 | }
16 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/java/com/xg/chat/view/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat.view;
2 |
3 | import android.content.Intent;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.os.Bundle;
6 | import android.text.TextUtils;
7 | import android.view.View;
8 | import android.widget.EditText;
9 |
10 | import com.xg.chat.R;
11 | import com.xg.chat.utils.ToastUtils;
12 |
13 | import butterknife.Bind;
14 | import butterknife.ButterKnife;
15 | import butterknife.OnClick;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 | @Bind(R.id.etHost)
19 | EditText etHost;
20 | @Bind(R.id.etProt)
21 | EditText etProt;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_main);
27 | ButterKnife.bind(this);
28 |
29 | }
30 |
31 | @OnClick(R.id.btnAdd)
32 | public void onClick(View v) {
33 | String host = etHost.getText().toString().trim();
34 | String prot = etProt.getText().toString().trim();
35 | if (TextUtils.isEmpty(host) || TextUtils.isEmpty(prot)) {
36 | ToastUtils.makeToast(this, "信息不能为空!");
37 | return;
38 | }
39 | Intent intent = new Intent(this, ChatRoomActivity.class);
40 | intent.putExtra("host", host);
41 | intent.putExtra("prot", prot);
42 | startActivity(intent);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_bg_text_hover.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_bg_text_hover.9.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_ic_bubble_no_left.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_ic_bubble_no_left.9.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_ic_bubble_no_right.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/drawable-xhdpi/rc_ic_bubble_no_right.9.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/layout/activity_chat_room.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
18 |
19 |
26 |
27 |
36 |
37 |
38 |
45 |
46 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
16 |
17 |
24 |
25 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/layout/lv_item_chat.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
21 |
22 |
28 |
29 |
30 |
39 |
40 |
46 |
47 |
48 |
57 |
58 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/android/XG-ChatRoom/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | XG-ChatRoom
3 |
4 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/android/XG-ChatRoom/src/test/java/com/xg/chat/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.xg.chat;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/images/20160416141020.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/images/20160416141020.png
--------------------------------------------------------------------------------
/images/20160416141118.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/images/20160416141118.png
--------------------------------------------------------------------------------
/images/20160416141148.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/images/20160416141148.png
--------------------------------------------------------------------------------
/images/20160416141205.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/images/20160416141205.png
--------------------------------------------------------------------------------
/images/20160416161021.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/images/20160416161021.png
--------------------------------------------------------------------------------
/java/Server/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/java/Server/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Server
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/java/Server/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding/=UTF-8
3 |
--------------------------------------------------------------------------------
/java/Server/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.8
12 |
--------------------------------------------------------------------------------
/java/Server/libs/gson-2.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/java/Server/libs/gson-2.3.1.jar
--------------------------------------------------------------------------------
/java/Server/src/com/xg/server/Main.java:
--------------------------------------------------------------------------------
1 | package com.xg.server;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.BufferedWriter;
5 | import java.io.ByteArrayOutputStream;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.InputStreamReader;
9 | import java.io.OutputStreamWriter;
10 | import java.io.PrintWriter;
11 | import java.net.ServerSocket;
12 | import java.net.Socket;
13 | import java.util.ArrayList;
14 | import java.util.List;
15 | import java.util.concurrent.ExecutorService;
16 | import java.util.concurrent.Executors;
17 |
18 | import com.google.gson.Gson;
19 | import com.xg.server.data.model.ChatBean;
20 |
21 | public class Main {
22 |
23 | private static final int PORT = 9999;
24 | private static List mList = new ArrayList();
25 | private static ServerSocket server = null;
26 | private static ExecutorService mExecutorService = null; //thread pool
27 |
28 | public static void main(String[] args) {
29 | try {
30 | server = new ServerSocket(PORT);
31 | mExecutorService = Executors.newCachedThreadPool(); //create a thread pool
32 | System.out.println("服务器已启动...");
33 | Socket client = null;
34 | while(true) {
35 | client = server.accept();
36 | //把客户端放入客户端集合中
37 | mList.add(client);
38 | mExecutorService.execute(new Service(client)); //start a new thread to handle the connection
39 | }
40 | }catch (Exception e) {
41 | e.printStackTrace();
42 | }
43 | }
44 | static class Service implements Runnable {
45 | private Socket socket;
46 | private BufferedReader in = null;
47 | private String msg = "";
48 |
49 | public Service(Socket socket) {
50 | this.socket = socket;
51 | try {
52 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
53 | //客户端只要一连到服务器,便向客户端发送下面的信息。
54 | } catch (IOException e) {
55 | e.printStackTrace();
56 | }
57 |
58 | }
59 |
60 | public byte[] readStream(InputStream inStream) throws Exception {
61 | ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
62 | byte[] buffer = new byte[1024];
63 | int len = -1;
64 | while ((len = inStream.read(buffer)) != -1) {
65 | outSteam.write(buffer, 0, len);
66 | }
67 | outSteam.close();
68 | inStream.close();
69 | return outSteam.toByteArray();
70 | }
71 |
72 | @Override
73 | public void run() {
74 | try {
75 | while(true) {
76 | if((msg = in.readLine())!= null) {
77 | System.out.println("接收:"+msg);
78 | ChatBean bean = new Gson().fromJson(msg, ChatBean.class);
79 | //当客户端发送的信息为:exit时,关闭连接
80 | if(bean.content.equals("exit")) {
81 | System.out.println("用戶:"+bean.name+"已退出谈论组");
82 | mList.remove(socket);
83 | in.close();
84 | socket.close();
85 | this.sendmsg();
86 | break;
87 | //接收客户端发过来的信息msg,然后发送给客户端。
88 | } else {
89 | this.sendmsg();
90 | }
91 | }
92 | }
93 | } catch (Exception e) {
94 | e.printStackTrace();
95 | }
96 | }
97 | /**
98 | * 循环遍历客户端集合,给每个客户端都发送信息。
99 | * 可以使用观察者模式设计讨论组
100 | */
101 | public void sendmsg() {
102 | System.out.println(msg);
103 | int num =mList.size();
104 | for (int index = 0; index < num; index ++) {
105 | Socket mSocket = mList.get(index);
106 | PrintWriter pout = null;
107 | try {
108 | pout = new PrintWriter(new BufferedWriter(
109 | new OutputStreamWriter(mSocket.getOutputStream())),true);
110 | pout.println(msg);
111 | }catch (IOException e) {
112 | e.printStackTrace();
113 | }
114 | }
115 | }
116 | }
117 |
118 | }
119 |
--------------------------------------------------------------------------------
/java/Server/src/com/xg/server/data/DiscussionGroups.java:
--------------------------------------------------------------------------------
1 | package com.xg.server.data;
2 |
3 | import java.net.Socket;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | public class DiscussionGroups{
8 |
9 | private static List mList = new ArrayList();
10 |
11 |
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/java/Server/src/com/xg/server/data/model/ChatBean.java:
--------------------------------------------------------------------------------
1 | package com.xg.server.data.model;
2 |
3 | /**
4 | * Created by Administrator on 2016/4/16 0016.
5 | */
6 | public class ChatBean {
7 | public String content;
8 | public String name;
9 | public long time;
10 |
11 | public ChatBean(String content, String name) {
12 | this.content = content;
13 | this.name = name;
14 | time = System.currentTimeMillis();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/java/Server/src/com/xg/server/socket/Service.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SunnyLine/Android-Socket-ChatRoom/f4417ab25fad28eeb43de104ce5dd6b3fb8f0698/java/Server/src/com/xg/server/socket/Service.java
--------------------------------------------------------------------------------