服务器端
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
public class MyPlayerServerActivity extends Activity {
/** Called when the activity is first created. */
private ServerSocket ss;
private boolean isRuning=true;
Socket s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread()
{
public void run()
{
try {
ss=new ServerSocket(8888);
while(isRuning)
{
s=ss.accept();
while(true)
{
DataInputStream dis=new DataInputStream(s.getInputStream());
short b=dis.readShort();
Log.v("a", "aaa");
Log.v("b", b+"");
Log.v("c", "ccc");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
try {
isRuning=false;
ss.close();
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("socket已关闭...", "socket已关闭...");
}
return false;
}
}
客户端
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyPlayerClientActivity extends Activity {
/** Called when the activity is first created. */
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
OutputStream os=null;
DataOutputStream dos=null;
Socket socket=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn4=(Button)findViewById(R.id.button4);
try {
socket=new Socket("192.168.1.101", 8888);
os=socket.getOutputStream();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
btn1.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
try {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putShort((short)1);
os.write(bytes);
bytes=null;
Toast.makeText(MyPlayerClientActivity.this, "已发送111...", Toast.LENGTH_SHORT).show();
// socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
btn2.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
try {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putShort((short)2);
os.write(bytes);
Toast.makeText(MyPlayerClientActivity.this, "已发送222...", Toast.LENGTH_SHORT).show();
// socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
btn3.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
try {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putShort((short)3);
os.write(bytes);
Toast.makeText(MyPlayerClientActivity.this, "已发送333...", Toast.LENGTH_SHORT).show();
// socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
btn4.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
try {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putShort((short)444);
os.write(bytes);
Toast.makeText(MyPlayerClientActivity.this, "已发送444...", Toast.LENGTH_SHORT).show();
// socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
}
}
服务端 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="服务器已启动111" />
</LinearLayout>
客户端main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>