綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[資料] Android與PC的socket通信

[複製連結] 查看: 2032|回覆: 3|好評: 1
跳轉到指定樓層
樓主
暗桌之光 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2011-7-24 11:52

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
  我們今天就來說說android怎麼樣才能和PC通信,這個我們就要用到socket了,那麼我們現在就來用一個小例子來看看。在代碼中我都用了註釋,希望大家來看看我發的帖子。

java代碼:
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;

  8. public class YaoChatServer extends Thread {

  9. private YaoChatServer() throws IOException {
  10. CreateSocket();
  11. //創建Socket服務器
  12. }

  13. public void run() {
  14. Socket client;
  15. String txt;
  16. try {
  17. while (true)
  18. //線程無限循環,實時監聽socket端口
  19. {
  20. client=ResponseSocket();
  21. //響應客戶端鏈接請求。。

  22. while(true)
  23. {
  24. txt=ReceiveMsg(client);
  25. System.out.println(txt);
  26. //鏈接獲得客戶端發來訊息,並將其顯示在Server端的螢幕上

  27. SendMsg(client,txt);
  28. //向客戶端返回訊息

  29. if(true)break;
  30. //中斷,繼續等待鏈接請求
  31. }

  32. CloseSocket(client);
  33. //關閉此次鏈接
  34. }
  35. }
  36. catch (IOException e) {
  37. System.out.println(e);
  38. }

  39. }

  40. private ServerSocket server = null;
  41. private static final int PORT = 5000;
  42. private BufferedWriter writer;
  43. private BufferedReader reader;

  44. private void CreateSocket() throws IOException
  45. {
  46. server = new ServerSocket(PORT, 100);
  47. System.out.println("Server starting..");
  48. }

  49. private Socket ResponseSocket() throws IOException
  50. {
  51. Socket client = server.accept();
  52. System.out.println("client connected..");

  53. return client;
  54. }

  55. private void CloseSocket(Socket socket) throws IOException
  56. {
  57. reader.close();
  58. writer.close();
  59. socket.close();
  60. System.out.println("client closed..");
  61. }

  62. private void SendMsg(Socket socket,String Msg) throws IOException
  63. {
  64. writer = new BufferedWriter(
  65. new OutputStreamWriter(socket.getOutputStream()));
  66. writer.write(Msg+"\n");
  67. writer.flush();

  68. }

  69. private String ReceiveMsg(Socket socket) throws IOException
  70. {
  71. reader = new BufferedReader(
  72. new InputStreamReader(socket.getInputStream()));
  73. System.out.println("server get input from client socket..");
  74. String txt="Sever send:"+reader.readLine();

  75. return txt;
  76. }

  77. public static void main(final String args[]) throws IOException {
  78. YaoChatServer yaochatserver = new YaoChatServer();
  79. if (yaochatserver != null) {
  80. yaochatserver.start();
  81. }
  82. }

  83. }
複製代碼
java代碼:
  1. package eoe.demo;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.net.UnknownHostException;

  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.net.Socket;
  9. import android.app.Activity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.*;


  13. public class YaoChatRoomAndroid extends Activity {
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);


  19. findviews();
  20. setonclick();

  21. }

  22. private EditText chattxt;
  23. private TextView chattxt2;
  24. private Button chatok;

  25. public void findviews()
  26. {
  27. chattxt = (EditText)this.findViewById(R.id.chattxt);
  28. chattxt2 = (TextView)this.findViewById(R.id.chattxt2);
  29. chatok = (Button)this.findViewById(R.id.chatOk);
  30. }

  31. private void setonclick()
  32. {
  33. chatok.setOnClickListener(new View.OnClickListener() {

  34. @Override
  35. public void onClick(View v) {
  36. try {
  37. connecttoserver(chattxt.getText().toString());
  38. } catch (UnknownHostException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. });
  47. }


  48. public void connecttoserver(String socketData) throws UnknownHostException, IOException
  49. {
  50. Socket socket=RequestSocket("192.168.10.119",5000);
  51. SendMsg(socket,socketData);
  52. String txt = ReceiveMsg(socket);
  53. this.chattxt2.setText(txt);
  54. }


  55. private Socket RequestSocket(String host,int port) throws UnknownHostException, IOException
  56. {
  57. Socket socket = new Socket(host, port);
  58. return socket;
  59. }

  60. private void SendMsg(Socket socket,String msg) throws IOException
  61. {
  62. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  63. writer.write(msg.replace("\n", " ")+"\n");
  64. writer.flush();
  65. }

  66. private String ReceiveMsg(Socket socket) throws IOException
  67. {
  68. BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

  69. String txt=reader.readLine();
  70. return txt;

  71. }
  72. }
複製代碼
java代碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/chattxt2"
  9. android:layout_width="319px"
  10. android:layout_height="68px"
  11. android:text="TextView"
  12. android:layout_alignParentTop="true"
  13. android:layout_alignParentLeft="true"
  14. >
  15. </TextView>
  16. <EditText
  17. android:id="@+id/chattxt"
  18. android:layout_width="319px"
  19. android:layout_height="52px"
  20. android:text="EditText"
  21. android:textSize="18sp"
  22. android:layout_below="@+id/widget30"
  23. android:layout_alignParentLeft="true"
  24. >
  25. </EditText>
  26. <Button
  27. android:id="@+id/chatOk"
  28. android:layout_width="320px"
  29. android:layout_height="41px"
  30. android:text="Button"
  31. android:layout_below="@+id/widget29"
  32. android:layout_alignParentLeft="true"
  33. >
  34. </Button>

  35. </LinearLayout>
複製代碼
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)

評分

參與人數 1幫助 +1 收起 理由
Jim_Cheng + 1

查看全部評分

收藏收藏6 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
沙發
514217 | 收聽TA | 只看該作者
發表於 2011-7-25 15:01
thxxxxxxxxxxxxxxxxxxxxxxxxxxx
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
s90752 | 收聽TA | 只看該作者
發表於 2011-10-10 11:35
我來玩玩看 看好不好用
感謝分享
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
wangmingte | 收聽TA | 只看該作者
發表於 2012-2-27 02:48
感謝分享!
大力的推..............................................

評分

參與人數 1幫助 +1 收起 理由
Jim_Cheng + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則