(1)BYTE TO STRING
(2)STRING TO BYTE
(3)THREAD
(4)HANDLE
- packagecom.FJICC.lzm;
- importjava.util.ArrayList;
- importjava.util.Enumeration;
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importjava.io.OutputStream;
- importjava.net.InetAddress;
- importjava.net.NetworkInterface;
- importjava.net.Socket;
- importjava.net.ServerSocket;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.app.Dialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- publicclasstcp_serverextendsActivity{
- privateButtonserverStart_btn;
- privateButtonserverStop_btn;
- privateTextViewreceivedata_tv;
- privateButtonsetport_btn;
- privateEditTextsenddata_et;
- privateButtonsend_btn;
- publicintPORT=8080;
- publicHandlermHandler;
- protectedstaticfinalintGUINOTIFIER=0x1234;
- @Override
- publicvoidonBackPressed(){
- //TODOAuto-generatedmethodstub
- super.onBackPressed();
- Intenti=newIntent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tcpserver_main);
- serverStart_btn=(Button)findViewById(R.id.btnStart);
- serverStop_btn=(Button)findViewById(R.id.btnStop);
- setport_btn=(Button)findViewById(R.id.btnSet);
- send_btn=(Button)findViewById(R.id.btnSend);
- senddata_et=(EditText)findViewById(R.id.et_send);
- receivedata_tv=(TextView)findViewById(R.id.tv_receive);
- serverStart_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- serverStart_btn.setEnabled(false);
- setport_btn.setEnabled(false);
- serverStop_btn.setEnabled(true);
- newThread()
- {
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- super.run();
- ServerSocketserverSocket=null;
- try{
- //创建ServerSocket对象监听PORT端口
- serverSocket=newServerSocket(PORT);
- //接收tcp连接返回socket对象
- Socketsocket=serverSocket.accept();
- //获得输入流
- InputStreaminputStream=socket.getInputStream();
- ///////////////////////////////////////////////////////////////////////////////////////
- //获得输出流
- OutputStreamoutputStream=socket.getOutputStream();
- byte[]byteBuffer=newbyte[1024];
- inttemp=0;
- Strings;
- //读取接收到的数据
- while((temp=inputStream.read(byteBuffer))!=-1)
- {
- outputStream.write(byteBuffer,0,temp);
- //将byte转为string
- //String(byte[],int,int)使用平台的缺省字符编码方式转换指定的字节子数组生成一新的String
- s=newString(byteBuffer,0,temp);
- //将string转byte
- //byte[]bs=str.getBytes();
- //定义一个message的变量m
- Messagem=newMessage();
- //消息的标记GUINOTIFIER在前面定义的
- m.what=tcp_server.GUINOTIFIER;
- //将要传送的数据传递给m.obj
- m.obj=s;
- //传送消息
- tcp_server.this.mHandler.sendMessage(m);
- }
- //System.out.println(newString(byteBuffer,0,temp));
- outputStream.flush();
- socket.close();
- serverSocket.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }.start();
- }
- });
- //创建handler
- mHandler=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){//得到Handle的通知了这个时候你可以做相应的操作
- casetcp_server.GUINOTIFIER://tcp_server是Activity的类名
- //清空textView
- receivedata_tv.setText("");
- //设置textView显示内容
- receivedata_tv.setText(msg.obj.toString());
- break;
- }
- super.handleMessage(msg);
- }
- };
- //结束TCP服务器
- serverStop_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- serverStart_btn.setEnabled(true);
- setport_btn.setEnabled(true);
- serverStop_btn.setEnabled(false);
- Intenti=newIntent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }});
- }
- }
- tcpserver_main.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnStart"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="开启服务"/>
- <Button
- android:id="@+id/btnStop"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="关闭服务"/>
- <Button
- android:id="@+id/btnSet"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="端口设置"/>
- </LinearLayout>
- <TextView
- android:id="@+id/tv_receive"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.70"
- android:text="TextView"/>
- <EditText
- android:id="@+id/et_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textMultiLine">
- <requestFocus/>
- </EditText>
- <Button
- android:id="@+id/btnSend"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="148dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="发送"/>
- </LinearLayout>
- <uses-permissionandroid:name="android.permission.INTERNET"/>