`

2013-08-09 android tcp server

 
阅读更多

永久链接: http://liuzongming1988.iteye.com/blog/1922747

 

(1)BYTE TO STRING

(2)STRING TO BYTE

(3)THREAD

(4)HANDLE

 

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

相关推荐

Global site tag (gtag.js) - Google Analytics