`

android tcp server

 
阅读更多

(1)BYTE TO STRING

(2)STRING TO BYTE

(3)THREAD

(4)HANDLE

 

  1. package com.FJICC.lzm;     
  2. import java.util.ArrayList;   
  3. import java.util.Enumeration;   
  4. import java.io.BufferedReader;   
  5. import java.io.IOException;   
  6. import java.io.InputStream;   
  7. import java.io.InputStreamReader;   
  8. import java.io.OutputStream;   
  9. import java.net.InetAddress;   
  10. import java.net.NetworkInterface;   
  11. import java.net.Socket;   
  12. import java.net.ServerSocket;   
  13.   
  14. import android.app.Activity;   
  15. import android.app.AlertDialog;   
  16. import android.app.Dialog;   
  17. import android.content.Context;   
  18. import android.content.DialogInterface;   
  19. import android.content.Intent;   
  20. import android.os.Bundle;   
  21. import android.os.Handler;   
  22. import android.os.Message;   
  23. import android.view.View;   
  24. import android.widget.Button;   
  25. import android.widget.EditText;   
  26. import android.widget.TextView;   
  27.   
  28.   
  29. public class tcp_server extends Activity{   
  30.        
  31. private Button serverStart_btn;   
  32. private Button serverStop_btn;   
  33. private TextView receivedata_tv;   
  34. private Button setport_btn;   
  35. private EditText senddata_et;   
  36. private Button send_btn;   
  37. public int PORT = 8080;   
  38. public Handler mHandler;   
  39. protected static final int GUINOTIFIER = 0x1234;   
  40.   
  41. @Override  
  42. public void onBackPressed() {   
  43.     // TODO Auto-generated method stub   
  44.     super.onBackPressed();   
  45.     Intent i =new Intent();   
  46.     i.setClass(tcp_server.this,MainActivity.class);   
  47.     startActivity(i);   
  48.     finish();   
  49. }   
  50. @Override  
  51. protected void onCreate(Bundle savedInstanceState) {   
  52.     // TODO Auto-generated method stub   
  53.     super.onCreate(savedInstanceState);   
  54.     setContentView(R.layout.tcpserver_main);   
  55.        
  56.     serverStart_btn=(Button)findViewById(R.id.btnStart);   
  57.     serverStop_btn=(Button)findViewById(R.id.btnStop);   
  58.     setport_btn=(Button)findViewById(R.id.btnSet);   
  59.     send_btn=(Button)findViewById(R.id.btnSend);   
  60.        
  61.     senddata_et=(EditText)findViewById(R.id.et_send);   
  62.     receivedata_tv=(TextView)findViewById(R.id.tv_receive);   
  63.   
  64.     serverStart_btn.setOnClickListener(new Button.OnClickListener(){   
  65.         @Override  
  66.         public void onClick(View v) {   
  67.             // TODO Auto-generated method stub   
  68.             serverStart_btn.setEnabled(false);   
  69.             setport_btn.setEnabled(false);   
  70.             serverStop_btn.setEnabled(true);   
  71.                
  72.             new Thread()   
  73.             {   
  74.                 @Override  
  75.                 public void run() {   
  76.                     // TODO Auto-generated method stub   
  77.                     super.run();   
  78.                     ServerSocket serverSocket=null;   
  79.                     try{   
  80.                         //创建ServerSocket对象监听PORT端口   
  81.                         serverSocket = new ServerSocket(PORT);   
  82.                         //接收tcp连接返回socket对象   
  83.                         Socket socket= serverSocket.accept();   
  84.                            
  85.                         //获得输入流   
  86.                         InputStream inputStream=socket.getInputStream();   
  87.                         ///////////////////////////////////////////////////////////////////////////////////////   
  88.                         //获得输出流   
  89.                         OutputStream outputStream = socket.getOutputStream();   
  90.                         byte []byteBuffer=new byte[1024];   
  91.                         int temp = 0;   
  92.                         String s;   
  93.                         //读取接收到的数据   
  94.                         while((temp = inputStream.read(byteBuffer))!=-1)   
  95.                             {   
  96.                             outputStream.write(byteBuffer, 0, temp);   
  97.                             //将byte转为string   
  98.                             //String(byte[], int, int)使用平台的缺省字符编码方式转换指定的字节子数组生成一新的 String    
  99.                             s = new String(byteBuffer,0,temp);   
  100.                                
  101.                             //将string转byte   
  102.                             //byte[] bs = str.getBytes();   
  103.                                
  104.                             //定义一个message的变量m   
  105.                             Message m = new Message();   
  106.                             //消息的标记GUINOTIFIER在前面定义的   
  107.                             m.what = tcp_server.GUINOTIFIER;   
  108.                             //将要传送的数据传递给 m.obj   
  109.                             m.obj =s;   
  110.                             //传送消息   
  111.                             tcp_server.this.mHandler.sendMessage(m);   
  112.                             }   
  113.                         //System.out.println(new String(byteBuffer,0,temp));   
  114.                         outputStream.flush();                          
  115.                         socket.close();   
  116.                         serverSocket.close();   
  117.                                                                            
  118.                     }catch(IOException e){   
  119.                         e.printStackTrace();   
  120.                     }   
  121.                 }                  
  122.             }.start();   
  123.         }      
  124.     });    
  125.   
  126.     //创建handler   
  127.     mHandler = new Handler() {   
  128.             public void handleMessage(Message msg) {   
  129.                 switch (msg.what) {//得到Handle的通知了 这个时候你可以做相应的操作   
  130.                     case tcp_server.GUINOTIFIER://tcp_server是Activity的类名          
  131.                         //清空textView   
  132.                         receivedata_tv.setText("");   
  133.                         //设置textView显示内容   
  134.                         receivedata_tv.setText(msg.obj.toString());   
  135.                         break;   
  136.                 }   
  137.                 super.handleMessage(msg);   
  138.             }   
  139.         };   
  140.         //结束TCP服务器   
  141.         serverStop_btn.setOnClickListener(new Button.OnClickListener(){   
  142.             @Override  
  143.             public void onClick(View v) {   
  144.                 serverStart_btn.setEnabled(true);   
  145.                 setport_btn.setEnabled(true);   
  146.                 serverStop_btn.setEnabled(false);   
  147.                    
  148.                 Intent i =new Intent();   
  149.                 i.setClass(tcp_server.this,MainActivity.class);   
  150.                 startActivity(i);   
  151.                 finish();   
  152.             }});   
  153. }   
  154.   
  155. }   
  156.   
  157.   
  158. tcpserver_main.xml   
  159. <?xml version="1.0" encoding="UTF-8"?>   
  160. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  161.     android:layout_width="fill_parent"  
  162.     android:layout_height="fill_parent"  
  163.     android:orientation="vertical" >   
  164.   
  165.     <LinearLayout   
  166.         android:layout_width="wrap_content"  
  167.         android:layout_height="wrap_content"  
  168.         android:layout_gravity="center_horizontal"  
  169.         android:orientation="horizontal" >   
  170.   
  171.         <Button   
  172.             android:id="@+id/btnStart"  
  173.             style="?android:attr/buttonStyleSmall"  
  174.             android:layout_width="wrap_content"  
  175.             android:layout_height="wrap_content"  
  176.             android:text="开启服务" />   
  177.   
  178.         <Button   
  179.             android:id="@+id/btnStop"  
  180.             style="?android:attr/buttonStyleSmall"  
  181.             android:layout_width="wrap_content"  
  182.             android:layout_height="wrap_content"  
  183.             android:text="关闭服务" />   
  184.   
  185.         <Button   
  186.             android:id="@+id/btnSet"  
  187.             style="?android:attr/buttonStyleSmall"  
  188.             android:layout_width="wrap_content"  
  189.             android:layout_height="wrap_content"  
  190.             android:text="端口设置" />   
  191.     </LinearLayout>   
  192.   
  193.     <TextView   
  194.         android:id="@+id/tv_receive"  
  195.         android:layout_width="match_parent"  
  196.         android:layout_height="wrap_content"  
  197.         android:layout_weight="0.70"  
  198.         android:text="TextView" />   
  199.   
  200.     <EditText   
  201.         android:id="@+id/et_send"  
  202.         android:layout_width="match_parent"  
  203.         android:layout_height="wrap_content"  
  204.         android:ems="10"  
  205.         android:inputType="textMultiLine" >   
  206.   
  207.         <requestFocus />   
  208.     </EditText>   
  209.   
  210.     <Button   
  211.         android:id="@+id/btnSend"  
  212.         style="?android:attr/buttonStyleSmall"  
  213.         android:layout_width="148dp"  
  214.         android:layout_height="wrap_content"  
  215.         android:layout_gravity="center_horizontal"  
  216.         android:text="发送" />   
  217.   
  218. </LinearLayout>   
  219.   
  220.   
  221.   
  222. <uses-permission android:name="android.permission.INTERNET"/> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics