之前一直會用sockets來實現emulator和PC進行通訊,卡了幾天,最後請教其他人終於可以連接了。 錯誤原因是在IP和端口,IP要用本機IP(在CMD輸入ipconfig,注意:每次開機本機IP都不同,ADSL是動態IP),端口用emulator的名稱,5554或5556.java sever文件要另外建java工程運行,在Android工程運行不了java(run application)
客戶端代碼:
Java代碼
- package com.yarin.android.Examples_08_04;
-
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
-
- public class Activity01 extends Activity
- {
- private final String DEBUG_TAG = "Activity01";
-
- private TextView mTextView=null;
- private EditText mEditText=null;
- private Button mButton=null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mButton = (Button)findViewById(R.id.Button01);
- mTextView=(TextView)findViewById(R.id.TextView01);
- mEditText=(EditText)findViewById(R.id.EditText01);
-
- //登陸
- mButton.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Socket socket = null;
- String message = mEditText.getText().toString() + "\r\n";
- try
- {
- //創建Socket
- socket = new Socket("116.29.27.138",5554); //查看本機IP,每次開機都不同
- //socket=new Socket("192.168.1.110",50000);
- //向服務器發送消息
- PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
- out.println(message);
-
- //接收來自服務器的消息
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String msg = br.readLine();
-
- if ( msg != null )
- {
- mTextView.setText(msg);
- }
- else
- {
- mTextView.setText("數據錯誤!");
- }
- //關閉流
- out.close();
- br.close();
- //關閉Socket
- socket.close();
- }
- catch (Exception e)
- {
- // TODO: handle exception
- Log.e(DEBUG_TAG, e.toString());
- }
- }
- });
- }
- }
- package com.yarin.android.Examples_08_04;
-
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
-
- public class Activity01 extends Activity
- {
- private final String DEBUG_TAG = "Activity01";
-
- private TextView mTextView=null;
- private EditText mEditText=null;
- private Button mButton=null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mButton = (Button)findViewById(R.id.Button01);
- mTextView=(TextView)findViewById(R.id.TextView01);
- mEditText=(EditText)findViewById(R.id.EditText01);
-
- //登陸
- mButton.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Socket socket = null;
- String message = mEditText.getText().toString() + "\r\n";
- try
- {
- //創建Socket
- socket = new Socket("116.29.27.138",5554); //查看本機IP,每次開機都不同
- //socket=new Socket("192.168.1.110",50000);
- //向服務器發送消息
- PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
- out.println(message);
-
- //接收來自服務器的消息
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String msg = br.readLine();
-
- if ( msg != null )
- {
- mTextView.setText(msg);
- }
- else
- {
- mTextView.setText("數據錯誤!");
- }
- //關閉流
- out.close();
- br.close();
- //關閉Socket
- socket.close();
- }
- catch (Exception e)
- {
- // TODO: handle exception
- Log.e(DEBUG_TAG, e.toString());
- }
- }
- });
- }
- }