Android 模拟器和PC用 sockets 通讯
2010-03-26 06:02:00 来源:WEB开发网之前一直会用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() + " ";
try
更多精彩
赞助商链接