你好,游客 登录 注册 搜索
背景:
阅读新闻

Android Socket通信(上)

[日期:2012-06-02] 来源:Linux社区  作者:htttw [字体:大 中 小]
2.
环境配置完毕并了解了基本原理后,直接上代码,下面是客户端的代码,用java实现:
src/BogoclientActivity.java
  1. package bogo.client.com;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintStream;  
  5. import java.net.Socket;  
  6. import java.net.UnknownHostException;  
  7.   
  8. import Android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15.   
  16. public class BogoclientActivity extends Activity  
  17. {  
  18.   /* 服务器地址 */  
  19.   private final String SERVER_HOST_IP = "192.168.1.2";  
  20.   
  21.   /* 服务器端口 */  
  22.   private final int SERVER_HOST_PORT = 9400;  
  23.     
  24.   private Button btnConnect;  
  25.   private Button btnSend;  
  26.   private EditText editSend;  
  27.   private Socket socket;  
  28.   private PrintStream output;  
  29.   
  30.   
  31.   public void toastText(String message)  
  32.   {  
  33.     Toast.makeText(this, message, Toast.LENGTH_LONG).show();  
  34.   }  
  35.   
  36.   public void handleException(Exception e, String prefix)  
  37.   {  
  38.     e.printStackTrace();  
  39.     toastText(prefix + e.toString());  
  40.   }  
  41.   
  42.   /** Called when the activity is first created. */  
  43.   @Override  
  44.   public void onCreate(Bundle savedInstanceState)  
  45.   {  
  46.     super.onCreate(savedInstanceState);  
  47.     setContentView(R.layout.main);  
  48.   
  49.     initView();  
  50.   
  51.     btnConnect.setOnClickListener(new Button.OnClickListener()  
  52.     {  
  53.       @Override  
  54.       public void onClick(View v)  
  55.       {  
  56.         initClientSocket();  
  57.       }  
  58.     });  
  59.       
  60.     btnSend.setOnClickListener(new Button.OnClickListener()  
  61.     {  
  62.       @Override  
  63.       public void onClick(View v)  
  64.       {  
  65.         sendMessage(editSend.getText().toString());  
  66.       }  
  67.     });  
  68.   }  
  69.     
  70.   public void initView()  
  71.   {  
  72.     btnConnect = (Button)findViewById(R.id.btnConnect);  
  73.     btnSend = (Button)findViewById(R.id.btnSend);  
  74.     editSend = (EditText)findViewById(R.id.sendMsg);  
  75.   
  76.     btnSend.setEnabled(false);  
  77.     editSend.setEnabled(false);  
  78.   }  
  79.   
  80.   public void closeSocket()  
  81.   {  
  82.     try  
  83.     {  
  84.       output.close();  
  85.       socket.close();  
  86.     }  
  87.     catch (IOException e)  
  88.     {  
  89.       handleException(e, "close exception: ");  
  90.     }  
  91.   }  
  92.     
  93.   private void initClientSocket()  
  94.   {  
  95.     try  
  96.     {  
  97.       /* 连接服务器 */  
  98.       socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);  
  99.   
  100.       /* 获取输出流 */  
  101.       output = new PrintStream(socket.getOutputStream(), true, "utf-8");  
  102.         
  103.       btnConnect.setEnabled(false);  
  104.       editSend.setEnabled(true);  
  105.       btnSend.setEnabled(true);  
  106.     }  
  107.     catch (UnknownHostException e)  
  108.     {  
  109.       handleException(e, "unknown host exception: " + e.toString());  
  110.     }  
  111.     catch (IOException e)  
  112.     {  
  113.       handleException(e, "io exception: " + e.toString());  
  114.     }  
  115.   }  
  116.     
  117.   private void sendMessage(String msg)  
  118.   {  
  119.     output.print(msg);  
  120.   }  
  121. }  

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/btnConnect"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/connect" />  
  17.   
  18.     <EditText  
  19.         android:id="@+id/sendMsg"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:inputType="text" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/btnSend"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="@string/send" />  
  29.   
  30. </LinearLayout>  

不要忘了,在AndroidManifest.xml中添加访问网络权限:
<uses-permission android:name="android.permission.INTERNET" />

把上面的代码编译并下载到模拟器中linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款