客户端程序:
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{