-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #803 from ChenHauyu/master
- Loading branch information
Showing
17 changed files
with
857 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...n/java/edu/hzuapps/androidworks/homeworks/net1314080903204/tcp_tester/AndroidManifest.xml
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,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="edu.hzuapps.andriodworks.homeworks.net1314080903204.tcp_tester"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@drawable/net1314080903204_icon" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".Net1314080903204_WelcomeActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity android:name=".Net1314080903204_SelectActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity> | ||
<activity android:name=".Net1314080903204_ServerActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity> | ||
<activity android:name=".Net1314080903204_ClientActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity> | ||
</application> | ||
|
||
</manifest> |
173 changes: 173 additions & 0 deletions
173
...s/androidworks/homeworks/net1314080903204/tcp_tester/Net1314080903204_ClientActivity.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,173 @@ | ||
package edu.hzuapps.andriodworks.homeworks.net1314080903204.tcp_tester; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Toast; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* Create By ChenHauyu | ||
* Email:[email protected] | ||
*/ | ||
|
||
/* 客户端界面代码 */ | ||
public class Net1314080903204_ClientActivity extends Activity { | ||
private final String TAG="Net1314080903204_ClientActivity"; | ||
private EditText edit_ip; | ||
private EditText edit_port; | ||
private EditText edit_send; | ||
private EditText edit_recv; | ||
private Button btn_connect; | ||
private Button btn_send; | ||
private boolean isConnected = false; | ||
Socket socket = null; | ||
BufferedWriter writer = null; | ||
BufferedReader reader = null; | ||
private String line; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState){ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.net1314080903204_activity_client); | ||
/* 初始化 */ | ||
edit_ip = (EditText) findViewById(R.id.edit_ip); | ||
edit_port = (EditText) findViewById(R.id.edit_port); | ||
edit_send = (EditText) findViewById((R.id.edit_msgsend)); | ||
edit_recv = (EditText) findViewById(R.id.edit_recv); | ||
btn_connect = (Button)findViewById(R.id.btn_connect); | ||
btn_send = (Button)findViewById(R.id.btn_send); | ||
/* 连接按钮 */ | ||
btn_connect.setOnClickListener(new View.OnClickListener(){ | ||
@Override | ||
public void onClick(View v) { | ||
/* 连接按钮处理函数 */ | ||
connect(); | ||
} | ||
}); | ||
/* 发送按钮 */ | ||
btn_send.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
/* 发送按钮处理函数 */ | ||
send(); | ||
} | ||
}); | ||
} | ||
|
||
/* 定义Handler对象 */ | ||
private Handler handler = new Handler() { | ||
@Override | ||
/* 当有消息发送出来的时候就执行Handler的这个方法 */ | ||
public void handleMessage(Message msg) { | ||
super.handleMessage(msg); | ||
/* 更新UI */ | ||
edit_recv.append(line); | ||
/* 调试输出 */ | ||
Log.i("PDA", "----->" + line); | ||
} | ||
}; | ||
|
||
/* 连接按钮处理函数:建立Socket连接 */ | ||
@SuppressLint("HandlerLeak") | ||
public void connect() { | ||
if(false == isConnected){ | ||
new Thread() { | ||
public void run(){ | ||
String IPAdr = edit_ip.getText().toString(); | ||
int PORT = Integer.parseInt(edit_port.getText().toString()); | ||
try { | ||
/* 建立socket */ | ||
socket = new Socket(IPAdr, PORT); | ||
/* 输出流 */ | ||
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); | ||
/* 输入流 */ | ||
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
/* 调试输出 */ | ||
Log.i(TAG, "输入输出流获取成功"); | ||
Log.i(TAG, "检测数据"); | ||
/* 读数据并更新UI */ | ||
char[] buf = new char[2048]; | ||
int i; | ||
while((i= reader.read(buf,0,100))!=-1) | ||
{ | ||
line = new String(buf,0,i); | ||
Message msg = handler.obtainMessage(); | ||
msg.obj = line; | ||
handler.sendMessage(msg); | ||
Log.i(TAG, "send to handler"); | ||
} | ||
} catch (UnknownHostException e){ | ||
Toast.makeText(Net1314080903204_ClientActivity.this,"无法建立连接:)",Toast.LENGTH_SHORT).show(); | ||
e.printStackTrace(); | ||
isConnected = false; | ||
} | ||
catch (IOException e) { | ||
Toast.makeText(Net1314080903204_ClientActivity.this,"无法建立连接:)",Toast.LENGTH_SHORT).show(); | ||
e.printStackTrace(); | ||
isConnected = false; | ||
} | ||
} | ||
}.start(); | ||
isConnected = true; | ||
/* 更新UI */ | ||
btn_connect.setText("断开"); | ||
Toast.makeText(Net1314080903204_ClientActivity.this,"连接成功:)",Toast.LENGTH_SHORT).show(); | ||
}else{ | ||
isConnected = false; | ||
/* 更新UI */ | ||
btn_connect.setText("连接"); | ||
edit_send.setText(""); | ||
edit_recv.setText(""); | ||
/* 关闭socket */ | ||
onDestroy(); | ||
Toast.makeText(Net1314080903204_ClientActivity.this,"连接已断开:)",Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
/* 发送按钮处理函数:向输出流写数据 */ | ||
public void send() { | ||
try { | ||
/* 向输出流写数据 */ | ||
writer.write(edit_send.getText().toString()+"\n"); | ||
writer.flush(); | ||
/* 更新UI */ | ||
edit_send.setText(""); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
try { | ||
/* 关闭socket */ | ||
if(null != socket){ | ||
socket.shutdownInput(); | ||
socket.shutdownOutput(); | ||
socket.getInputStream().close(); | ||
socket.getOutputStream().close(); | ||
socket.close(); | ||
} | ||
} catch (IOException e) { | ||
Log.d(TAG,e.getMessage()); | ||
} | ||
/* 更新UI */ | ||
btn_connect.setText("连接"); | ||
edit_recv.setText(""); | ||
super.onDestroy(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...s/androidworks/homeworks/net1314080903204/tcp_tester/Net1314080903204_SelectActivity.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,37 @@ | ||
package edu.hzuapps.andriodworks.homeworks.net1314080903204.tcp_tester; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
|
||
/** | ||
* Create By ChenHauyu | ||
* Email:[email protected] | ||
*/ | ||
|
||
/*功能选择界面代码*/ | ||
public class Net1314080903204_SelectActivity extends Activity { | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.net1314080903204_activity_select); | ||
/*“模拟服务端”按钮事件*/ | ||
findViewById(R.id.btn_toServer).setOnClickListener(new View.OnClickListener(){ | ||
@Override | ||
public void onClick(View v) { | ||
Intent intent = new Intent(Net1314080903204_SelectActivity.this,Net1314080903204_ServerActivity.class); //页面跳转至服务端界面 | ||
startActivity(intent); | ||
} | ||
}); | ||
/*“模拟客户端”按钮事件*/ | ||
findViewById(R.id.btn_toClient).setOnClickListener(new View.OnClickListener(){ | ||
@Override | ||
public void onClick(View v) { | ||
Intent intent = new Intent(Net1314080903204_SelectActivity.this,Net1314080903204_ClientActivity.class); //页面跳转至客户端界面 | ||
startActivity(intent); | ||
} | ||
}); | ||
} | ||
} | ||
|
Oops, something went wrong.