forked from hzuapps/android-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
i. sshe
committed
Apr 20, 2016
1 parent
376d1df
commit 22466a9
Showing
4 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
.../main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Activity/ClientThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.example.dell.multichat; | ||
|
||
import android.os.Looper; | ||
import android.os.Message; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.net.SocketTimeoutException; | ||
import android.os.Handler; ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
//import java.util.logging.Handler; | ||
|
||
import java.util.logging.LogRecord; | ||
|
||
/** | ||
* Created by dell on 2016/4/12. | ||
*/ | ||
public class ClientThread implements Runnable { | ||
private Socket s; | ||
private Handler handler; //定义向UI发送消息的Handler对象 | ||
public Handler revHandler; //定义接收UI线程消息的Handler对象 | ||
BufferedReader br =null; //输入流 | ||
OutputStream os = null; //输出流 | ||
|
||
public ClientThread(Handler handler) | ||
{ | ||
this.handler = handler; | ||
} | ||
|
||
public void run() | ||
{ | ||
try { | ||
System.out.println("Socket before!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); | ||
s = new Socket("192.168.240.19", 30000); | ||
br = new BufferedReader((new InputStreamReader(s.getInputStream()))); | ||
os = s.getOutputStream(); //为什么br不是类似这样? | ||
|
||
//启动一条子线程来读取服务器响应的数据 | ||
new Thread() | ||
{ | ||
@Override | ||
public void run() | ||
{ | ||
String content = null; | ||
//不断读取Socket输入流中的内容 | ||
try | ||
{ | ||
while((content = br.readLine()) != null) | ||
{ | ||
//读到来自服务器的数据后, 发送消息通知程序 | ||
//界面显示该数据 | ||
Message msg = new Message(); | ||
msg.what = 0x123; //????? | ||
msg.obj = content; //???? | ||
handler.sendMessage(msg); //??? | ||
} | ||
} | ||
catch (IOException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
}.start(); //???? | ||
|
||
//当前线程初始化 | ||
Looper.prepare(); //???? | ||
revHandler = new Handler() | ||
{ | ||
@Override | ||
public void handleMessage(Message msg) | ||
{ | ||
//接收到UI线程中用户输入的数据 | ||
if (msg.what == 0x345) | ||
{ | ||
//将用户在文本框输入的内容写入网络 | ||
try | ||
{ | ||
os.write((msg.obj.toString() + "\r\n").getBytes("utf-8")); | ||
//并写到历史文件,格式:时间+【名称】+ 消息 | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}; | ||
//启动Looper | ||
Looper.loop(); | ||
} | ||
catch (SocketTimeoutException e1) | ||
{ | ||
System.out.println("网络连接超时!!!"); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
.../java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Activity/MultiThreadClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.example.dell.multichat; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.os.Message; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
|
||
//import java.util.logging.Handler; | ||
import android.os.Handler; | ||
/** | ||
* Created by dell on 2016/4/12. | ||
*/ | ||
public class MultiThreadClient extends Activity { | ||
//定义界面的两个文本框 | ||
EditText input; | ||
TextView show; | ||
//定义一个界面上的按钮 | ||
Button send; | ||
Handler handler; | ||
//定义与服务器通信的子线程 | ||
ClientThread clientThread; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) | ||
{ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.main); | ||
input = (EditText) findViewById(R.id.input); | ||
send = (Button) findViewById(R.id.send); | ||
show = (TextView) findViewById(R.id.show); | ||
|
||
handler = new Handler() //????不懂!!! | ||
{ | ||
@Override | ||
public void handleMessage(Message msg) | ||
{ | ||
//如果消息来自于子线程 | ||
if (msg.what == 0x123) //????不懂!!! | ||
{ | ||
//将读取的内容追加显示到文本框 | ||
show.append("\n" + msg.obj.toString()); | ||
//并写到文件! //!!!! | ||
} | ||
} | ||
}; | ||
clientThread = new ClientThread(handler); | ||
//客户端启动ClientThread线程创建网络连接、读取来自服务器的数据 | ||
new Thread(clientThread).start(); | ||
|
||
send.setOnClickListener(new View.OnClickListener() ///!!! | ||
{ | ||
@Override | ||
public void onClick(View v) | ||
{ | ||
try { | ||
//当用户按下发送按钮后, 将用户输入的数据封装成Message | ||
//然后发送给子线程的Handler | ||
Message msg = new Message(); | ||
msg.what = 0x345; ///???????不懂!!! | ||
clientThread.revHandler.sendMessage(msg); | ||
|
||
//清空input文本框 | ||
input.setText(""); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); ///note!!!!不懂!!! | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...c/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Activity/ReadHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.dell.multichat; | ||
|
||
//import java.io.*; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.FileNameMap; | ||
|
||
/** | ||
* Created by dell on 2016/4/18. | ||
*/ | ||
|
||
/*读历史记录,如果没有指定目录或文件,只需直接返回, | ||
不需要创建, 创建会在写历史记录的时候执行 | ||
*/ | ||
public class ReadHistory { | ||
|
||
public void file_read() | ||
{ | ||
File get_path = this.getAbsolutePath(); // | ||
String path_str = get_path.toString() + "/history"; | ||
File path = new File(path_str); | ||
String file_str = path_str + "/ChatHistory"; | ||
File file = new File(path_str, "ChatHistory"); | ||
|
||
|
||
//如果目录不存在或者目录不存在, 什么也不做 | ||
if (!path.exists() || !file.exists()){ | ||
//nothing to do | ||
} | ||
|
||
//两个都存在,这读取历史记录,显示到show区域。一行一行存储历史记录。 | ||
|
||
BufferedReader read_file = null; | ||
try { | ||
read_file = new BufferedReader(new InputStreamReader(new FileInputStream(file_str))); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
String line_data = null; | ||
try { | ||
while ((line_data = read_file.readLine()) != null) | ||
{ | ||
//显示到show区域 | ||
|
||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
.../main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Activity/WriteHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.dell.multichat; | ||
|
||
/** | ||
* Created by dell on 2016/4/18. | ||
*/ | ||
public class WriteHistory { | ||
|
||
public void write_file() | ||
{ | ||
//判断历史记录目录是否存在,不存在则创建 | ||
|
||
//判断历史记录文件是否存在, 不存在则创建 | ||
|
||
//获取时间+历史记录写入文件 ; 从服务器接收到的信息写入文件 | ||
// (自己发送的信息不用写入,因为服务器还会返回发送出去的信息,如果再写会有重复) | ||
|
||
} | ||
|
||
} |