客户端程序:
package wyf.wpf; //声明包语句import java.io.DataInputStream; //引入相关类
import java.io.DataOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket; //引入相关类
import android.app.Activity; //引入相关类
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle; //引入相关类
import android.view.View;
import android.widget.Button;
import android.widget.EditText; //引入相关类
public class Sample_10_1_Client extends Activity {
private String msg;
private EditText et;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Socket socket;
DataOutputStream dout = null;
DataInputStream din = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //设置当前屏幕
 et = (EditText)findViewById(R.id.et); //获得EditText对象
 button1= (Button)findViewById(R.id.Button01);
 button2= (Button)findViewById(R.id.Button02);
 button3= (Button)findViewById(R.id.Button03);
 button4= (Button)findViewById(R.id.Button04);
 button5= (Button)findViewById(R.id.Button05);
button1.setOnClickListener(new buttonListener1());
button2.setOnClickListener(new buttonListener2());
button3.setOnClickListener(new buttonListener3());
button4.setOnClickListener(new buttonListener4());
button5.setOnClickListener(new buttonListener5());
        connectToServer(); //连接服务端
    }
    class buttonListener1 implements OnClickListener, android.view.View.OnClickListener{
     public void onClick(View v) {
     String str="one";
     try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
     }
     public void onClick(DialogInterface dialog, int which) {
    
     }    
       }
    class buttonListener2 implements OnClickListener, android.view.View.OnClickListener{
     public void onClick(View v) {
     String str="two";
     try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
     }
     public void onClick(DialogInterface dialog, int which) {
    
     }    
       }
    class buttonListener3 implements OnClickListener, android.view.View.OnClickListener{
     public void onClick(View v) {
     String str="three";
     try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
     }
     public void onClick(DialogInterface dialog, int which) {
    
     }    
       }
    class buttonListener4 implements OnClickListener, android.view.View.OnClickListener{
     public void onClick(View v) {
     String str="four";
     try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
     }
     public void onClick(DialogInterface dialog, int which) {
    
     }    
       }
    class buttonListener5 implements OnClickListener, android.view.View.OnClickListener{
     public void onClick(View v) {
     String str="five";
     try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
     }
     public void onClick(DialogInterface dialog, int which) {
    
     }    
       }
    public void connectToServer(){ //方法:连接客户端
     try{
      socket = new Socket("127.0.0.1", 9958);//创建Socket对象  10.200.53.56  60.213.232.43
//msg = new PrintWriter(socket.getOutputStream());
      din = new DataInputStream(socket.getInputStream()); //获得DataInputStream对象
      dout = new DataOutputStream(socket.getOutputStream()); //获得DataInputStream对象
//din.read(msg.getBytes("UTF-8"));//getBytes("UTF-8")
     //msg = din.readUTF();   //读取服务端发来的消息
     //et.setText(msg); //设置EditText对象\
     //button1.setText(msg);
     }
     catch(Exception e){ //捕获并打印异常
     e.printStackTrace();
     }
    }
}

解决方案 »

  1.   

    两步就可以了:步骤:
    1、打开百度搜索“IP查询”,然后会有一个本机IP: xx.xx.xx.xx  2、将以下代码中的127.0.0.1改成步骤1查到的本机IP:xx.xx.xx.xx
    socket = new Socket("127.0.0.1", 9958);
      

  2.   

    socket = new Socket("127.0.0.1", 9958);//创建Socket对象 10.200.53.56 60.213.232.43
    不是这个9958端口,把这个port转化为16进制,再交换高低位。
    即:port = ((port& 0xFF00)>>8)|(port& 0x00FF)<<8);