android实现的TcpServerWord文件下载.docx
- 文档编号:15888421
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:11
- 大小:88.59KB
android实现的TcpServerWord文件下载.docx
《android实现的TcpServerWord文件下载.docx》由会员分享,可在线阅读,更多相关《android实现的TcpServerWord文件下载.docx(11页珍藏版)》请在冰豆网上搜索。
LinearLayoutxmlns:
android="
android:
layout_width="
fill_parent"
layout_height="
orientation="
vertical"
>
<
TextView
id="
@+id/txt_port"
wrap_content"
text="
Port:
"
/>
EditText
@+id/edit_port"
1234"
Button
@+id/btn_connect"
Listen"
TextView
@+id/txt_receive"
Receive:
EditText
@+id/edit_receive"
lines="
2"
@+id/txt_send"
Send:
@+id/edit_send"
1"
@+id/btn_send"
Send"
/LinearLayout>
//TcpServerActivity.java文件里的代码
packagecom.lujianfei.tcpserver;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
import.ServerSocket;
import.Socket;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
publicclassTcpServerActivityextendsActivity{
privatefinalStringTAG="
TcpServerActivity"
;
//Abouttheuicontrols
privateEditTextedit_port=null;
privateButtonbtn_connect=null;
privateEditTextedit_receive=null;
privateEditTextedit_send=null;
privateButtonbtn_send=null;
privatebooleanisConnected=false;
//Aboutthesocket
privateServerSocketserverSocket=null;
privatestaticSocket[]client=null;
privatefinalintMAXSIZE=2;
//Setthemaximumconnectionsfortheserver
privateIntegerclient_index=0;
privateOutputStreamoutputStream=null;
privateInputStreaminputStream=null;
privatebooleanthread_flag=true;
privatebooleanthread_read_flag=true;
StringBuilderrec_str=null;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
privatevoidinitControls(){
edit_port=(EditText)findViewById(R.id.edit_port);
btn_connect=(Button)findViewById(R.id.btn_connect);
btn_connect.setOnClickListener(newbtn_connect_onClickListener());
edit_receive=(EditText)findViewById(R.id.edit_receive);
edit_send=(EditText)findViewById(R.id.edit_send);
btn_send=(Button)findViewById(R.id.btn_send);
btn_send.setOnClickListener(newbtn_send_onClickListener());
client=newSocket[MAXSIZE];
client_index=newInteger(0);
protectedvoidonDestroy(){
//TODOAuto-generatedmethodstub
isConnected=false;
thread_flag=false;
try{
if(null!
=serverSocket){
serverSocket.close();
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
Log.d(TAG,e.getMessage());
}
btn_connect.setText("
Connect"
);
edit_receive.setText("
super.onDestroy();
//Threadtoreaddatasfromclients
classReadThreadimplementsRunnable{
intindex;
publicReadThread(intindex){
this.index=index;
publicvoidrun(){
byte[]data=newbyte[1024];
//TODOAuto-generatedmethodstub
try{
while(true){
inputStream=client[index].getInputStream();
intreadBytes=inputStream.read(data);
Log.d(TAG,"
index:
+index+"
readBytes:
+readBytes+"
data:
+newString(data,0,readBytes));
Log.d(TAG,"
from:
"
+client[index].getRemoteSocketAddress().toString());
if(readBytes==0)
continue;
rec_str.append(newString(data,0,readBytes));
runOnUiThread(newRunnable(){
publicvoidrun(){
//TODOAuto-generatedmethodstub
edit_receive.setText(rec_str.toString());
}
});
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
Log.d(TAG,"
ReadThread:
while(thread_flag)"
+e.getMessage());
thread_read_flag=false;
}finally{
try{
inputStream.close();
}catch(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 实现 TcpServer